Beginner

Hello World in Ruby

Your first Ruby program - the classic Hello World example with Docker setup

Every programming journey starts with Hello World. Ruby makes this delightfully simple—it’s just one line that reads almost like English.

The Code

Create a file named hello.rb:

1
puts "Hello, World!"

That’s it. One line. This elegant simplicity is what makes Ruby a joy to write.

Understanding the Code

  • puts - Short for “put string,” this method outputs text to the console and adds a newline
  • "Hello, World!" - A string literal enclosed in double quotes
  • No semicolons needed - Ruby uses newlines to separate statements
  • No boilerplate - Unlike Java or C, there’s no class or main function required

Alternative Ways to Output

Ruby provides several methods for output:

1
2
3
puts "Hello, World!"   # Adds newline at end
print "Hello, World!"  # No automatic newline
p "Hello, World!"      # Debug output with quotes visible

Running with Docker

The easiest way to run this without installing Ruby locally:

1
2
3
4
5
# Pull the official Ruby Alpine image (lightweight)
docker pull ruby:3.3-alpine

# Run the program
docker run --rm -v $(pwd):/app -w /app ruby:3.3-alpine ruby hello.rb

Running Locally

If you have Ruby installed:

1
2
# Run directly
ruby hello.rb

Expected Output

Hello, World!

Why Ruby is Different

Compare Ruby’s Hello World to other languages:

LanguageLines of CodeBoilerplate Required
Ruby1None
Python1None
Java5Class, main method
C4includes, main function

Ruby and Python share this minimal approach, reflecting their shared philosophy that simple things should be simple.

Key Concepts

  1. Ruby is interpreted - No compilation step; source runs directly
  2. Everything is an object - Even strings and numbers have methods
  3. Dynamic typing - No need to declare variable types
  4. Expressive syntax - Ruby code often reads like English

Interactive Ruby (IRB)

Ruby also has a REPL (Read-Eval-Print Loop) called IRB. Try it:

1
docker run --rm -it ruby:3.3-alpine irb

Then type:

1
2
3
4
irb(main):001> puts "Hello, World!"
Hello, World!
=> nil
irb(main):002> exit

The => nil shows the return value of puts (which returns nothing). This interactive mode is great for experimenting.

Ruby’s Philosophy

Matz (Ruby’s creator) designed the language around programmer happiness:

“I hope to see Ruby help every programmer in the world to be productive, and to enjoy programming, and to be happy.”

This philosophy shows even in Hello World—clean, readable, no fuss.

Next Steps

Continue to Variables and Data Types to learn about storing and manipulating data in Ruby.

Running Today

All examples can be run using Docker:

docker pull ruby:3.3-alpine
Last updated: