Introduction to Ruby

Reviewed & published by Brayan K

Ruby is a dynamic, beginner-friendly programming language designed for programmer happiness and readable code that reads almost like plain English.

Part of the free Ruby course at LearnCodingFast — hands-on lessons with worked examples and the output they print, plus practice exercises and a quick quiz.

By the end of this lesson you'll understand what Ruby is and why it exists, how it runs your code, and you'll have written and run your own first Ruby script that prints to the screen.

What You'll Learn in This Lesson

1️⃣ What is Ruby?

Ruby is a dynamic, object-oriented language created in Japan by Yukihiro "Matz" Matsumoto and first released in 1995. Matz blended pieces he loved from other languages and added a single guiding principle: the language should make programmers happy. That philosophy shows up everywhere:

You'll start where every Ruby developer starts: a tiny script that prints text. Read the worked example below — every line is explained — then run it yourself.

# This is a Ruby script. Ruby reads it top to bottom and runs each line.
# Lines starting with # are comments — Ruby ignores them.

# puts prints a line of text and adds a newline at the end.
puts "Hello from Ruby!"        # -> Hello from Ruby!
puts "Programmer happiness."   # -> Programmer happiness.

# Ruby does real work too. Math behaves just as you'd expect.
total = 2 + 2
puts "2 + 2 = #{total}"        # -> 2 + 2 = 4

# In Ruby, almost everything is an object — even a number.
puts 42.class                  # -> Integer

2️⃣ Why Ruby Feels Different: Readable, Object-Oriented Code

Two things make Ruby feel friendly. First, the syntax stays out of your way — blocks use do...end and conditions can even trail the line they affect. Second, because everything is an object, you call methods on values directly, and many method names end in ? (a question, returns true/false) or ! (a warning, changes things). Read the comments, predict each line's output, then check.

# Ruby is famous for reading almost like English.
# Notice there are no semicolons and no curly braces around the block.

3.times do
  puts "Ruby is friendly!"
end

# "unless" is the opposite of "if" — it runs when the condition is false.
hungry = false
puts "Time to eat" if hungry        # prints nothing, hungry is false
puts "Keep coding" unless hungry    # -> Keep coding

# Method names can end in ? or ! to hint at what they do.
puts "ruby".upcase                  # -> RUBY
puts "".empty?                      # -> true

Notice how puts "Keep coding" unless hungry reads almost like a sentence. That trailing unless is pure Ruby flavour — it keeps simple guards short and clear.

Your turn. The script below works once you replace the two TODO markers. Follow the 👉 hints, then run it and compare with the expected output.

# 🎯 YOUR TURN — replace each ___, then run it with: ruby greet.rb

# 1) Store your name in a variable (text goes in "double quotes")
name = ___             # 👉 replace ___ with "Sam" (or your own name, in quotes)

# 2) Print a greeting that includes the variable.
#    Ruby drops a value into a string with #{...} inside double quotes.
puts "Hello, #{name}! Welcome to Ruby."

# 3) Print which version of Ruby you are running.
#    RUBY_VERSION is a built-in constant Ruby gives you.
puts "You are on Ruby #{RUBY_VERSION.split(".").first}.x"   # RUBY_VERSION is the full "3.3.6"-style string

# ✅ Expected output (for "Sam"):
# Hello, Sam! Welcome to Ruby.
# You are on Ruby 3.x

3️⃣ Running a File with the ruby Command

To run a Ruby script on your own machine, save it in a file ending in .rb (for example hello.rb) and type ruby hello.rb in your terminal. Ruby reads the file top to bottom and prints anything you puts to the screen.

# First, check Ruby is installed (prints the version number):
ruby -v

# Save your code in a file called hello.rb, then run it:
ruby hello.rb

# Ruby runs the file and prints whatever you puts:
# (the lines below are the program's output)

Mini-Challenge: A "Script Info" Report

No blanks this time — just a brief and an outline. Write it yourself, run it with ruby info.rb, and check your output against the example in the comments.

# 🎯 MINI-CHALLENGE: a tiny "about this script" report.
# Save it as info.rb and run it with: ruby info.rb
#
# 1. Print a friendly title line of your own.
# 2. Print the Ruby version using RUBY_VERSION.
# 3. Print a small calculation using string interpolation #{...},
#    e.g. how many minutes are in a day.
# 4. Use puts for every line.
#
# ✅ Example output:
#    === Script Info ===
#    Ruby version: 3.3.0
#    Minutes in a day: 1440

# your code here

Common Errors (and the fix)

Pro Tips

📋 Quick Reference — Ruby Basics

ConceptRuby Syntax
Run a fileruby app.rb
Print a lineputs "hi"
Check Ruby versionruby -v
Version (in code)RUBY_VERSION
Interpolate a value"hi #{name}"
Interactive shellirb

🎉 Lesson Complete!

Practice quiz

Who created Ruby and what was its guiding goal?

  • Matz, for programmer happiness
  • Guido, for readability
  • Larry Wall, for flexibility
  • Brendan Eich, for the browser

Answer: Matz, for programmer happiness. Yukihiro Matsumoto (Matz) designed Ruby around programmer happiness.

Which method prints a line of text and adds a trailing newline?

  • print
  • puts
  • echo
  • write

Answer: puts. puts adds a newline; print does not.

What does puts 42.class output?

  • 42
  • Number
  • Integer
  • Fixnum

Answer: Integer. In Ruby everything is an object; a whole number is an Integer.

Which syntax interpolates a value into a string?

  • $(name)
  • {name}
  • #{name} inside double quotes
  • %name%

Answer: #{name} inside double quotes. String interpolation uses #{...} inside double quotes.

Does string interpolation work inside single quotes?

  • yes, always
  • no, only double quotes
  • only with a backslash
  • only for numbers

Answer: no, only double quotes. Single quotes print #{...} literally; use double quotes.

How do you run a Ruby script named hello.rb from the terminal?

  • run hello.rb
  • ruby hello.rb
  • exec hello.rb
  • rb hello.rb

Answer: ruby hello.rb. The ruby command followed by the filename runs the script.

What does 3.times do ... end do?

  • runs the block 3 times
  • returns the number 3
  • counts to 3 and stops the program
  • raises an error

Answer: runs the block 3 times. times is an iterator that runs the block that many times.

What does "".empty? return?

  • false
  • true
  • nil
  • 0

Answer: true. An empty string is empty, so empty? returns true.

By convention, a method name ending in ? usually:

  • mutates the object
  • returns true or false
  • raises an exception
  • is private

Answer: returns true or false. A trailing ? marks a predicate that returns a boolean.

Which built-in constant holds the running Ruby version?

  • VERSION
  • RUBY_VERSION
  • RubyVersion
  • ENV['RUBY']

Answer: RUBY_VERSION. RUBY_VERSION is the built-in constant for the interpreter version.

Continue this course

Frequently asked questions

What exactly is Ruby — and what is it good for?

Ruby is a dynamic, object-oriented programming language created by Yukihiro Matsumoto (Matz) in the mid-1990s with one guiding goal: programmer happiness. It is designed so that code reads almost like plain English, which makes it a wonderful first language. Ruby powers everything from small automation scripts and command-line tools to large web applications — most famously through Ruby on Rails, the web framework that launched companies like GitHub, Shopify, and Airbnb.

Is Ruby a good language for complete beginners?

Yes. Ruby's syntax is clean and forgiving: there are no semicolons to remember, blocks read naturally with do...end, and the language tries hard to do what you mean. Because almost everything is an object with friendly methods (like "hello".upcase or 5.times), you can experiment and get instant feedback. Many people learn programming fundamentals — variables, methods, objects — more quickly in Ruby than in lower-level languages.

How do I install and run Ruby?

On macOS and Linux, Ruby often comes pre-installed, but the best way to manage versions is a tool like rbenv or RVM. On Windows, use RubyInstaller from rubyinstaller.org. Once installed, check it with ruby -v in your terminal, then save code in a file ending in .rb and run it with ruby filename.rb. You can also try Ruby instantly online at try.ruby-lang.org without installing anything.

What does it mean that 'everything is an object' in Ruby?

In many languages, simple values like numbers or true/false are treated specially. In Ruby they are full objects with methods you can call. That is why 42.class returns Integer, "hi".length returns 2, and (1..5).sum returns 15. This consistency means once you learn how to call methods, the whole language behaves the same way — there are very few special cases to memorise.