Hello World program in Ruby language

Writing a Hello World program in Ruby is as simple as writing a single line in a file as

puts 'Hello World'

Run the Ruby program

Create a file name as hello.rb in your favorite editor or through command line editor like vim.

Although, you can give any name to file. I have used hello. By convention, Ruby source files have .rb file extension.

Then, type following line in the file:

puts 'Hello World' # or puts "Hello World"
Ruby uses '#' symbol as single line comment.

Run the program in the terminal or shell as:

ruby hello.rb

You should be able to see output as:

Hello World
Congrats! You just wrote your first program in Ruby.

Self-evaluate your learning

Ruby uses // for for single line comments?

1. true
2. false

We use .ruby as our extension for Ruby programs.

1. true
2. false

Key points to understand

  • File extension of Ruby program is .rb, or .rbw
    • The .rbw extension is used for “windowed” applications.
  • To print strings into the standard output (i.e terminal), you can use puts, which means “put string”.
  • # is used as a comment mark.
    • This does not have to be at the beginning of a line.
    • This can be used on the same line after a Ruby statement.
  • =begin is the start of a mult-line comment.
  • =end is the closing of a mult-line comment.