Getting Started with Ruby: Hello World
Ruby is known for its simplicity and readability. Let’s start with the most basic program: printing “Hello, World!” to the screen.
The Simple Way
The most straightforward way to write Hello World in Ruby is:
puts "Hello, World!"
That’s it! Just one line of code.
A More Interactive Version
Here’s a slightly more interactive version that asks for your name:
print "What's your name? "
name = gets.chomp
puts "Hello, #{name}!"
How it works:
printdisplays text without a new linegetsreads user inputchompremoves the trailing newline#{name}interpolates the variable in the string
Running the Code
- Save either version in a file (e.g.,
hello.rb) - Open your terminal
- Run:
ruby hello.rb
That’s your first step into Ruby programming! Simple, clean, and elegant - that’s the Ruby way.
Happy coding! 🚀