RbRuby
BeginnerLesson 1 of 16

Hello World, puts & irb – Ruby

Your first Ruby program and the idea "everything is an object".

A Ruby program is a text file with the extension .rb. With puts you print a line, print leaves out the line break, and p shows a value in debug form (with quotation marks for strings).

puts "Hello World!"
print "No line break"
print "\n"
p "p shows the inspect representation"
puts 1 + 2
puts "I'm learning", "Ruby"     # each argument on its own line

Parentheses on method calls are optional. Comments start with #. And because everything is an object, you can call methods directly on numbers, strings and even nil:

# Single-line comment
=begin
Multi-line comment
=end
puts 5.class                  # Integer
puts "abc".upcase             # ABC
puts -5.abs                   # 5
puts 3.times.to_a.inspect     # [0, 1, 2]
puts nil.to_a.inspect         # []
puts nil.class                # NilClass
TipStart irb and try everything right away. The result of the last expression is displayed automatically (=> 42). In Ruby the value of the last line is always the result – more on that later with methods.
Warningputs nil prints an empty line, whereas p nil prints nil. And puts [1, 2] prints the elements on separate lines – not the square brackets. For the debug view use p or .inspect.