Hello World in Ruby

Watch out! This tutorial is over 8 years old. Please keep this in mind as some code snippets provided may no longer work or need modification to work on current systems.
Tutorial Difficulty Level    

This is how you would display “Hello World” in Ruby.

Create a text file called hello_world.rb containing the following code:

puts 'Hello, world!'

Now run it at the shell prompt.

$ ruby hello_world.rb
Hello, world!

But wait, there’s more…

Interactive Ruby

Ruby also comes with a program that will show the results of any Ruby statements you feed it. Playing with Ruby code in interactive sessions like this is a terrific way to learn the language.

Open up IRB (which stands for Interactive Ruby).

  • If you’re using Mac OS X open up Terminal and type irb, then hit enter.
  • If you’re using Linux, open up a shell and type irb and hit enter.
  • If you’re using Windows, open Interactive Ruby from the Ruby section of your Start Menu (if available).
irb(main):001:0>

Ok, so it’s open. Now what?

Type this: "Hello World"

irb(main):001:0> "Hello World"
=> "Hello World"

Ruby Obeyed You!

What just happened? Did we just write the world’s shortest “Hello World” program? Not exactly. The second line is just IRB’s way of telling us the result of the last expression it evaluated. If we want to print out “Hello World” we need a bit more:

irb(main):002:0> puts "Hello World"
Hello World
=> nil

puts is the basic command to print something out in Ruby. But then what’s the => nil bit? That’s the result of the expression. puts always returns nil, which is Ruby’s absolutely-positively-nothing value.