Hello World in Ruby

Watch out! This tutorial is over 4 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:

Now run it at the shell prompt.

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).
Ok, so it’s open. Now what?

Type this: "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:

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.