#---Hello World---
Our first program will print the classic “hello world” message.
Here’s the full source code:
puts 'hello world!'
To run this, copy this single line code to a new
plaintext file. Save the file as “hello.rb” and run
it using the ruby
command like so from the terminal:
ruby hello.rb
.
This should print:
hello world!
Congrats! You’ve run your first ruby program and you did it without importing any library or defining a class or declaring a variable.
The puts
method is typically how you’d print something
to the standard output.
An empty puts
prints an empty line.
You can also use p
instead of puts
.
It’s shorter and provides extra info that’s useful
while debugging:
puts '42' # => 42
p '42' # => "42"
puts "a\bb" # => b
p "a\bb" # => "a\bb"
Both puts
and p
are ruby methods from the Kernel
module which is included by default implicitly in every
ruby program.
(Use the left and right arrow keys to navigate the topics.)