Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

RB
Ruby
0/12 completed
Beginner
Hello RubyVariables and typesConditions and loopsMethods
Intermediate
Arrays and hashesBlocks, procs and iteratorsClasses and objectsModules and mixins
Advanced
Exceptions and filesMetaprogramming
Pro
Gems, Bundler and testingRuby on Rails basics
Tracks / Ruby / Beginner

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

Ruby · editable

That's the whole program: no main function, no class, no imports. puts ("put string") prints a value followed by a newline.

Ruby · editable
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:

Ruby · editable

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

Ruby · editable

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:

Ruby · editable

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:

Ruby · editable

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

  1. Install Ruby:
    • macOS: brew install ruby (or a version manager like rbenv or mise)
    • Ubuntu/Debian: sudo apt install ruby-full
    • Windows: the RubyInstaller from rubyinstaller.org, or WSL
  2. Check it: ruby -v
  3. Save a program as hello.rb and run it: ruby hello.rb
  4. Try code interactively in IRB (Interactive Ruby): type irb, then any expression, and see the result straight away. Type exit to leave.
$ irb
irb(main):001> 2 ** 10
=> 1024
irb(main):002> "ruby".reverse
=> "ybur"

Ruby's style

  • Names use snake_case for variables and methods, CamelCase for classes, and SCREAMING_CASE for constants.
  • Indent with 2 spaces.
  • No semicolons at line ends, and no parentheses around if conditions.
  • 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

  1. Print your name, your city and the current year in one sentence using interpolation.
  2. Use p and puts on the same array [1, "two", nil] and compare the output.
  3. Ask for two numbers with gets and print their sum and product.
  4. Find three methods you can call on a string (try "hello".methods.sort.first(40)).
✎ Check your understanding

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?