Hello Ruby
Ruby was created in 1995 by Yukihiro "Matz" Matsumoto in Japan, with one unusual goal: to make programmers happy. Ruby code reads almost like English, there's usually a pleasant way to say what you mean, and everything is an object. It powers Ruby on Rails, the framework behind GitHub, Shopify, Airbnb (in its early days), Basecamp and thousands of startups, and it's a favourite for scripts, automation and command-line tools.
💡 The editors in this track run real Ruby 4.0 (the official CRuby, compiled to WebAssembly) right in your browser. The first time you press Run, about 8 MB is downloaded; after that it starts instantly. Change any example and run it again: that's the fastest way to learn.
Your first program
That's the whole program: no main function, no class, no imports. puts ("put string") prints a value followed by a newline.
| Method | Prints |
|---|---|
puts |
The value and a newline (arrays: one element per line) |
print |
The value, no newline |
p |
The value as you'd write it in code (strings get quotes). It's the best tool for debugging |
"#{...}" inside double quotes inserts any expression into a string. That's called interpolation, and you'll use it constantly.
Everything is an object
In Ruby, even numbers and nil are objects with methods you can call:
Methods ending in ? return true/false by convention (even?, empty?, include?). You can chain calls, and parentheses around arguments are optional when the meaning is clear: puts("hi") and puts "hi" are the same.
Comments
Reading input
gets reads one line typed by the user (here, from the Input box under the editor). It includes the newline, so chomp removes it:
gets.to_i turns the text into an integer. Change the values in the Input box and run it again.
Errors
When something goes wrong, Ruby stops and tells you the file, line, method and error class:
ZeroDivisionError is the error's class. Read error messages from the top: the first line says what happened and where.
Running Ruby on your computer
- Install Ruby:
- macOS:
brew install ruby(or a version manager likerbenvormise) - Ubuntu/Debian:
sudo apt install ruby-full - Windows: the RubyInstaller from rubyinstaller.org, or WSL
- macOS:
- Check it:
ruby -v - Save a program as
hello.rband run it:ruby hello.rb - Try code interactively in IRB (Interactive Ruby): type
irb, then any expression, and see the result straight away. Typeexitto leave.
$ irb
irb(main):001> 2 ** 10
=> 1024
irb(main):002> "ruby".reverse
=> "ybur"Ruby's style
- Names use
snake_casefor variables and methods,CamelCasefor classes, andSCREAMING_CASEfor constants. - Indent with 2 spaces.
- No semicolons at line ends, and no parentheses around
ifconditions. - Blocks of code end with
end.
These conventions are followed almost universally (and enforced by the RuboCop tool), so Ruby code from anyone looks familiar.
Practice
- Print your name, your city and the current year in one sentence using interpolation.
- Use
pandputson the same array[1, "two", nil]and compare the output. - Ask for two numbers with
getsand print their sum and product. - Find three methods you can call on a string (try
"hello".methods.sort.first(40)).
1.What does puts "2 + 2 = #{2 + 2}" print?
2.Which method shows a string with its quotes, useful for debugging?
3.What does gets.chomp do?
4.In Ruby, what is 5?