Skip to content

Language guide

Learn Ruby

An expressive language known for the Rails web framework and readable code.

Ruby on SkillAIVibe

Runs in your browser

Real Ruby runs inside a sandbox in your own browser — CRuby 3.4.1 via @ruby/wasm-wasi 2.10.1. Nothing you write is sent to a server. Last verified against the sandbox's checks on .

10 exercises in order, each teaching exactly one new idea. Every one runs in this tab, checks your output, and explains what went wrong in plain language.

  1. Hello, Out Loudputs prints text followed by one newline
  2. Name TagA variable stores a value, and #{...} drops it into a string
  3. Party PlannerInteger division truncates with /, and % gives the remainder
  4. Umbrella or Notif / elsif / else chooses between several outcomes
  5. Countdowndownto repeats a block once for every number in a falling range
  6. The Clapping Gamean if inside a loop is decided again on every pass
  7. The Scoreboardljust and rjust pad a string to a fixed width so columns line up
  8. Packing ListAn array holds several values in order, and each walks through them
  9. Rainfall Weeka variable created before a loop carries a running total from pass to pass
  10. The Bike Rental ReceiptAssembling a complete program out of parts you already know

What Ruby is

Ruby is a general-purpose language designed so that code reads pleasantly and says what it means: method calls often need no brackets, blocks let you hand a chunk of code to a method, and almost everything, including numbers, is an object you can ask questions of. It is interpreted, so you run a file directly, and it comes with an interactive prompt (irb) for trying ideas one line at a time. Most people meet Ruby through the Rails web framework, but the language stands on its own for scripts, tools and teaching.

Where Ruby is used

Web applications with Rails
Ruby on Rails is a complete web framework, covering the database, routing, views, email and background jobs, and it remains the main reason people learn Ruby.
Command-line tools and automation
Ruby's string methods, regular expressions and file handling make it comfortable for scripts that rename, parse, generate or tidy things up.
Developer and infrastructure tooling
Package managers, configuration-management tools and static site generators have been written in Ruby, and their plugin systems expect you to write Ruby too.
Testing and internal libraries
Testing frameworks such as RSpec and Minitest are known for readable test code, and many teams package their internal libraries as gems.
Teaching and prototyping
Because a working idea takes few lines and the error messages are clear, Ruby is used to sketch programs and to teach programming concepts.

Your first Ruby program

Saved as hello.rb. You can paste it straight into the playground to see it run.

Ruby
name = "Tomas"
year = 2026
items = ["tea", "bread", "rice"]

puts "Hello, #{name}."
puts "In five years it will be #{year + 5}."
puts "You have #{items.length} items on your list:"
items.each { |item| puts "- #{item}" }

What it prints

Output
Hello, Tomas.
In five years it will be 2031.
You have 3 items on your list:
- tea
- bread
- rice
  1. Lines 1 to 3 create variables. There is no keyword and no type to declare; Ruby works out the type from the value. The square brackets make an array.
  2. Line 5 prints a line with puts, which adds a newline for you. Inside a double-quoted string, #{...} drops a value into the text; this is called interpolation.
  3. Line 6 shows that any expression can go inside #{...}, not only a variable name, so the addition happens in place.
  4. Line 7 calls a method on the array with a dot: items.length asks the array how many things it holds. items.size and items.count would give the same answer.
  5. Line 8 is the most Ruby-flavoured line. each runs the block between the braces once per item, and |item| names the current value inside the block.

Try it in the Ruby playground →

Run Ruby on your own computer

Ruby is interpreted, so once it is installed the whole workflow is a text file and one command. Installation varies more by operating system than it does for most languages, which is why the installation page on ruby-lang.org is the right place to start.

  1. Install Ruby

    Follow the installation page on ruby-lang.org for your system. On Windows the usual route is the installer that page links to; on macOS and Linux most people use either the package from their package manager or a version manager, both of which the page describes. Any 3.x release is suitable.

  2. Check the version

    Open a terminal and ask Ruby for its version. If your system came with an old 2.x Ruby, install a current one alongside it rather than relying on the built-in copy.

    Shell
    ruby --version
  3. Run a file

    Save the first program above as hello.rb and run it from the folder it is in.

    Shell
    ruby hello.rb
  4. Try the interactive prompt

    irb comes with Ruby. Type an expression such as 3.times { puts "hi" } and press Enter to see what it does; it is the quickest way to find out how a method behaves. Type exit to leave.

    Shell
    irb
  5. Install a gem when you need one

    Libraries are called gems and are installed with the gem command that ships with Ruby. Bundler, also included, records the gems a project needs in a file called Gemfile so that anyone can install the same set.

A learning order for Ruby

Stages, not a timetable. Each one exists because the next would not make sense without it, and how long each takes depends on how much you write.

  1. Stage 1. Output, variables and strings

    • puts, print and p
    • variables and assignment
    • string interpolation
    • numbers and arithmetic
    • gets for input

    The three printing methods behave differently, and knowing which one to use is the first small Ruby-specific thing to learn. Everything else starts with storing a value and showing it.

  2. Stage 2. Conditions and loops

    • if / elsif / else and unless
    • comparison and truthiness
    • while and until
    • times, upto and each
    • case / when

    Ruby's rule that only nil and false count as false is different from many languages and needs to be learned early. The loop methods on numbers and collections replace most traditional for loops.

  3. Stage 3. Collections and blocks

    • arrays
    • hashes and symbols
    • each, map, select and reduce
    • blocks, procs and yield
    • ranges

    Blocks are the heart of Ruby style. Once you can pass a block to map or select, most data processing becomes a short chain of readable calls.

  4. Stage 4. Methods and objects

    • def and implicit return
    • default and keyword arguments
    • classes and initialize
    • attr_accessor and instance variables
    • modules and mixins

    Everything in Ruby is an object, so defining your own classes is how you model the things your program is about. Modules let behaviour be shared without deep inheritance trees.

  5. Stage 5. Errors, files and gems

    • reading a backtrace
    • begin / rescue / ensure
    • File.read and File.write
    • JSON and CSV from the standard library
    • gem and Bundler

    Programs that touch real files and real data fail in real ways. Handling that, and pulling in a library instead of writing everything yourself, is where scripts turn into tools.

  6. Stage 6. Testing, Rails and beyond

    • Minitest or RSpec
    • a first Rails application
    • models, views and controllers
    • how a web request is handled
    • a project of your own

    Rails assumes you know the language, which is why it comes last. With the earlier stages in place, its conventions read as sensible defaults rather than magic.

Mistakes beginners make in Ruby

Forgetting an end
Every def, if, while, class and do-block closes with end. Miss one and Ruby reports a syntax error at the end of the file, with a message about unexpected end-of-input, far from the real gap. Indent consistently and count your ends when the error appears.
Calling a method on nil
NoMethodError: undefined method 'upcase' for nil is the most common Ruby error. It means a variable you expected to hold a value holds nil, often because a hash key does not exist or a search found nothing. Look at where the value came from rather than at the line that failed.
Treating 0 or an empty string as false
In Ruby only nil and false are false. if 0 and if "" both run their body, which surprises people coming from other languages. Test for what you actually mean: value.zero?, value.empty? or value.nil?.
Writing = when == was meant
if count = 5 assigns 5 to count and is always true. Ruby prints a warning (found '= literal' in conditional, should be ==), but only a warning, so the program still runs. Read warnings when they appear.
Starting a variable name with a capital letter
A name that begins with a capital is a constant, not a variable. Assigning it a second time produces warning: already initialized constant, and assigning it inside a method is a syntax error (dynamic constant assignment). Variables are snake_case in Ruby; capitals are for classes and constants.

Strengths and trade-offs

Where it is strong

  • Code that reads close to English: 3.times, list.each, unless done, and method names that end in a question mark when they answer a question.
  • Blocks make working with collections short and clear, and the same pattern carries into files, timing, database transactions and much else.
  • Rails is a complete, well-documented way to build a database-backed web application, and many conventions it introduced are now common in other frameworks.
  • Clear error messages and an interactive prompt that make experimenting cheap.

Where it is not

  • Slower than compiled languages for CPU-heavy work. Most web applications spend their time waiting on databases and networks, which is why this rarely matters for Rails, but it rules Ruby out for number crunching and games.
  • Outside web development and tooling the ecosystem is thin: mobile, desktop and data science are served much better by other languages.
  • Ruby's flexibility (classes can be reopened, methods added while the program runs) makes large codebases harder to follow and harder for tools to check. Type signatures exist (RBS ships with Ruby) but are optional.
  • Activity is concentrated around Rails. If you do not want to build web applications, much of the Ruby world will not be relevant to you.

Who Ruby is for

Ruby suits people who want to build web applications and who care about how code reads; if a Rails application is your goal, or you will be maintaining one, it is the obvious choice. It is also a pleasant first language, with the caveat that most learning material assumes you are heading for Rails. Look elsewhere if you want mobile apps, games, data science or high-performance systems. If you are choosing between Ruby and Python as a general first language with no web project in mind, Python's wider reach into data and automation is the practical tiebreaker.

Questions about learning Ruby

Is Ruby the same thing as Ruby on Rails?
No. Ruby is the language; Rails is a web framework written in Ruby that gives you a structured way to build database-backed websites. You can write Ruby without ever touching Rails, for scripts, command-line tools or terminal games, and you should learn the language basics first, because Rails assumes them.
Is Ruby a good first language?
It is a gentle one: little punctuation, forgiving syntax and readable error messages. Its main drawback as a first language is direction rather than difficulty. Most Ruby learning material points at web development, so if that is not where you want to go, the path is less well trodden than it is for some other languages.
Which Ruby version should I install?
The current 3.x series. Ruby publishes maintenance and end-of-life dates for each version on ruby-lang.org, so check that the version your system offers is still supported. Most code written for Ruby 2.x runs unchanged on 3.x, but the reverse is not always true, so learn on a current release.
Is Ruby slow?
For CPU-bound work, yes, compared with compiled languages, and recent Ruby versions have added a just-in-time compiler to narrow the gap. For the typical web application, the database and the network dominate and the language's speed is rarely the bottleneck. If your project is heavy computation, choose a compiled language; if it is a website or a script, Ruby's speed is fine.

The primary source

When this guide and the official documentation disagree, the documentation is right and we would like to hear about it.

Other languages

All languages and paths · Programming glossary · Your progress