Getting Started with Ruby: Your First Programs
Getting started with Ruby is quick and rewarding. In this tutorial, you will write your first Ruby program and get comfortable with Ruby’s clean, expressive syntax. If you have not installed Ruby yet, check out our Installing Ruby guide first.
intro context
If this is your first Ruby tutorial, the goal is not to memorize syntax all at once. The goal is to get a feel for how Ruby reads, how it runs a file, and how the language combines small pieces into readable programs. Once that starts to feel normal, the rest of Ruby becomes much easier to pick up.
Ruby rewards small experiments. You can change a line, run the file again, and immediately see the result. That feedback loop is part of why the language is such a friendly place to start.
If you want a reliable reference while you practice, keep the official Ruby documentation open in another tab. The language reference is useful when you want to double-check syntax details, but this tutorial should give you enough context to read the examples and start making small changes with confidence.
TL;DR
Use this tutorial to get a first working Ruby file on your machine, practice simple expressions, and learn the basic syntax that appears throughout the rest of the fundamentals series. If you remember nothing else, remember that Ruby favors readable code and lets you build up from small, testable examples.
What makes Ruby special
Ruby was created in 1995 by Yukihiro Matsumoto with a simple philosophy: programming should be fun for humans. The result is a language that reads almost like English and emphasizes developer happiness over complexity.
Here is what makes Ruby stand out:
- Elegant syntax; Ruby code tends to be concise and readable
- Everything is an object; Even primitive types like numbers have methods
- Dynamic typing; No need to declare variable types
- Blocks; A powerful way to pass code to methods
Your first Ruby program
The traditional Hello, World! program in Ruby is beautifully simple:
puts "Hello, World!"
That is it! Save this to a file called hello.rb and run it with the ruby command. Ruby reads the file, executes each line in order, and prints the result to your terminal. This is the simplest complete Ruby program you can write:
ruby hello.rb
You should see the output appear on the next line. The program writes a single string and exits. The ruby command reads the file, compiles it internally, and runs every statement from top to bottom, printing output to the terminal:
Hello, World!
The puts method prints text to the screen. Notice there is no need for semicolons at the end of lines; Ruby figures it out automatically.
Running Ruby code
There are two main ways to run Ruby code:
1. from a file
Create a file with the .rb extension and run it with the ruby command:
ruby your_file.rb
This runs your program and prints any output to the terminal. Files are the standard way to save and re-run Ruby programs, and you will use them throughout this tutorial series.
2. interactive Ruby (irb)
For experimentation, use IRB, Ruby’s interactive console. IRB reads one line at a time, evaluates it, and prints the result immediately. This makes it perfect for testing ideas before committing them to a file:
irb
You will see a prompt where you can type Ruby code and see immediate results. The arrow (=>) shows the return value of each expression. IRB is especially useful for checking method behavior, testing small snippets, and exploring unfamiliar libraries without creating a file first. Try a few expressions to get a feel for the feedback loop:
irb> 2 + 2
=> 4
irb> "hello".upcase
=> "HELLO"
Type exit or press Ctrl+D to leave IRB. Keep an IRB session open while you work through this tutorial; it is the fastest way to test small ideas without creating a new file each time. IRB is also useful for exploring unfamiliar libraries without creating a file first.
Basic Ruby syntax
Let us cover the fundamentals you will use in every Ruby program. These building blocks appear in every script, from one-liners to full applications, and they form the vocabulary you will reach for every time you open a Ruby file.
Variables
Assign values to variables using the = operator. Ruby figures out the type automatically based on the value you provide. A variable is just a name that points to an object, and the same name can refer to different objects over time:
name = "Alice"
age = 25
price = 19.99
is_active = true
Ruby uses snake_case for variable names (first_name, not firstName). Variable names should describe what the value represents, not what type it is. Ruby’s dynamic typing means the same variable can hold different types at different times, though that is rarely a good practice in real code.
Data types
Ruby has several built-in data types that cover the most common programming needs. Each type comes with its own set of methods you can call directly on the value:
# Strings
greeting = "Hello"
name = 'Alice'
# Numbers
count = 42
temperature = 98.6
# Booleans
is_ready = true
is_complete = false
# Arrays
colors = ["red", "green", "blue"]
# Hashes
person = { name: "Alice", age: 25 }
Strings hold text, numbers can be integers or floats, booleans represent true and false, arrays store ordered lists, and hashes map keys to values. Every one of these types is an object in Ruby, which means you can call methods on them directly.
Comments
Use # for single-line comments. Everything after the hash on that line is ignored by the interpreter:
# This is a comment - Ruby ignores it
puts "Hello" # This comment is after code
Comments after code are useful for annotating return values or explaining why a particular approach was chosen. Keep them focused on the “why” rather than restating the “what” that the code already makes clear.
Arithmetic operations
Ruby handles math naturally with standard operators. You can mix integers and floats, and Ruby will choose the appropriate result type:
sum = 10 + 5 # 15
difference = 10 - 5 # 5
product = 10 * 5 # 50
quotient = 10 / 5 # 2
remainder = 10 % 3 # 1
Division between two integers returns an integer in Ruby, truncating the decimal portion. Use a float dividend (10.0 / 5) if you need the fractional result.
Methods
Define reusable code with the def keyword. Methods let you name a block of logic and call it from anywhere in your program:
def greet(name)
"Hello, #{name}!"
end
puts greet("Alice") # Hello, Alice!
puts greet("Bob") # Hello, Bob!
The #{expression} syntax inserts values into strings; this is called string interpolation.
Conditionals
Make decisions with if, elsif, and else:
score = 85
if score >= 90
puts "A - Excellent!"
elsif score >= 80
puts "B - Good job!"
elsif score >= 70
puts "C - Satisfactory"
else
puts "Need improvement"
end
Ruby also supports unless for negative conditions. unless is equivalent to if not but reads more naturally when the negative case is the one you want to handle:
unless user_signed_in?
redirect_to login_path
end
Loops
Repeat actions with loops. Ruby prefers iterator-style loops over traditional for constructs because they are more expressive and work naturally with blocks:
# Print numbers 1 to 5
5.times do |i|
puts "Count: #{i + 1}"
end
Or iterate over collections:
colors = ["red", "green", "blue"]
colors.each do |color|
puts color
end
The each method walks through every element in the array and hands each one to the block. Ruby collections all respond to each, so the pattern stays the same whether you are walking an array, a hash, or a range.
Why learn Ruby?
Ruby has been powering web applications for over two decades. Here is why developers love it:
Readable code
Ruby code reads almost like natural English. Compare this JavaScript:
const users = data.filter(user => user.active).map(user => user.name);
To Ruby:
users = data.select(&:active).map(&:name)
Both do the same thing, but Ruby tends to be more concise. The select and map methods express the intent directly without requiring explicit callback functions. Ruby’s block syntax and symbol-to-proc shorthand make collection pipelines especially compact. That combination of readability and maturity is why Ruby remains a strong choice for web apps, scripts, and automation.
The Ruby community
Ruby has a passionate community that values:
- Developer happiness
- Convention over configuration
- Beautiful code over clever code
The Ruby on Rails framework, released in 2004, revolutionized web development and remains popular today.
That combination of readability and maturity is why Ruby remains a good beginner language even after you move beyond the basics. The syntax stays approachable, but the ecosystem is large enough that you can keep using Ruby for web apps, scripts, and automation without switching languages every time the project changes shape.
If you are coming from another language, the biggest payoff is not just the syntax. Ruby encourages you to think in small, composable pieces, which means you can read code, change one line, and understand the result without a long setup process.
Job opportunities
Ruby developers are in demand for:
- Web development with Ruby on Rails
- Scripting and automation
- DevOps tools (Chef, Vagrant are written in Ruby)
- API development
Setting up your environment
Before you write more Ruby code, make sure your environment is ready. Having the right Ruby version and a comfortable editor removes friction from the learning process and lets you focus on the code instead of the tools:
Check your Ruby version
ruby --version
You should see something like ruby 3.2.0 or newer. If the version is older, consider upgrading so you have access to recent language features and performance improvements. Most Ruby 3.x releases include meaningful speedups and new syntax conveniences that make everyday coding smoother. A current Ruby version also ensures the examples in this tutorial work as written.
Use a code editor
We recommend an editor with Ruby support for syntax highlighting, auto-completion, and inline error checking:
- VS Code with the Ruby extension for syntax highlighting and debugging
- RubyMine for full IDE features including refactoring and test running
- Sublime Text for a lightweight option with Ruby syntax support
Summary
Ruby is a readable, object-oriented language that makes it easy to start with simple programs and gradually move toward larger applications. If you understand how to run a file, print output, use variables, and experiment in IRB, you already have the foundation you need for the rest of the fundamentals track.
That foundation also makes it easier to read the rest of this series without stopping every few lines to decode syntax. The next topics build on the same habits: keep the examples small, run them often, and notice how Ruby treats values, methods, and objects as part of the same language story.
If you want a good way to keep going, pick one small script and rewrite it two or three times while changing only one feature at a time. That kind of repetition helps Ruby syntax feel familiar much faster than reading alone, and it turns the language into something you can shape instead of something you just observe.
forward-link
Continue with Ruby Variables and Types to see how Ruby stores data, then move to Ruby Methods and Ruby Strings to practice building slightly larger programs.
Join the community
Help is available at:
- Ruby Forum (ruby-forum.com)
- Stack Overflow
- Reddit r/ruby
- Discord servers
Next steps
You have written your first Ruby programs and learned the basics. Here is what to explore next:
- Variables and Types; Deep dive into how Ruby handles data
- Control Flow; Master if/else, loops, and case statements
- Methods; Create reusable code blocks
- Strings; Work with text effectively
Ruby is a language that rewards curiosity. The best way to learn is by writing code, breaking things, and experimenting. Pick the next tutorial that interests you, and keep building!