Ruby: Output


Example: Basic Output in Ruby


	puts "Hello World!";
	

Output:

  Hello World!


Example: Basic Output in Ruby


	print <<EOF
	   This is a paragraph of text 
	   spanning multiple lines.
	EOF
	

Output:

    This is a paragraph of text <     spanning multiple lines.


Reserved Words

The following are reserved words in Ruby

  • BEGIN
  • END
  • alias
  • and
  • begin
  • break
  • case
  • class
  • def
  • defined?
  • do
  • else
  • elsif
  • end
  • ensure
  • false
  • for
  • if
  • in
  • module
  • next
  • nil
  • not
  • or
  • redo
  • rescue
  • retry
  • return
  • self
  • super
  • then
  • true
  • undef
  • unless
  • until
  • when
  • while
  • __FILE__
  • __LINE__

Example: BEGIN command

The BEGIN command declares code that is called and run at the outset.


	puts "Main Program"

	BEGIN {
	   puts "Initialization code - runs first."
	}
	

Output:

    Initialization code - runs first.
    Main Program


Example: END command and comments/remarks in Ruby

The END command declares code that is called and run at the end of the program.


	puts "Main Program"

	END {
	   puts "Run this at the end."
	}
	

Output:

    Main Program
    Run this at the end.