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:
| |
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:
| |
Running with Docker
The easiest way to run this without installing Ruby locally:
| |
Running Locally
If you have Ruby installed:
| |
Expected Output
Hello, World!
Why Ruby is Different
Compare Ruby’s Hello World to other languages:
| Language | Lines of Code | Boilerplate Required |
|---|---|---|
| Ruby | 1 | None |
| Python | 1 | None |
| Java | 5 | Class, main method |
| C | 4 | includes, main function |
Ruby and Python share this minimal approach, reflecting their shared philosophy that simple things should be simple.
Key Concepts
- Ruby is interpreted - No compilation step; source runs directly
- Everything is an object - Even strings and numbers have methods
- Dynamic typing - No need to declare variable types
- Expressive syntax - Ruby code often reads like English
Interactive Ruby (IRB)
Ruby also has a REPL (Read-Eval-Print Loop) called IRB. Try it:
| |
Then type:
| |
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.