Getting Started with Ruby
Welcome to Ruby! In this tutorial, you will write your first Ruby program and get comfortable with the language clean, expressive syntax. If you have not installed Ruby yet, check out our Installing Ruby guide first.
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:
ruby hello.rb
You should see:
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
2. Interactive Ruby (IRB)
For experimentation, use IRB — Ruby interactive console:
irb
You will see a prompt where you can type Ruby code and see immediate results:
irb> 2 + 2
=> 4
irb> "hello".upcase
=> "HELLO"
Type exit or press Ctrl+D to leave IRB.
Basic Ruby Syntax
Let us cover the fundamentals you will use in every Ruby program.
Variables
Assign values to variables using the = operator:
name = "Alice"
age = 25
price = 19.99
is_active = true
Ruby uses snake_case for variable names (first_name, not firstName).
Data Types
Ruby has several built-in data types:
# 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 }
Comments
Use # for single-line comments:
# This is a comment - Ruby ignores it
puts "Hello" # This comment is after code
Arithmetic Operations
Ruby handles math naturally:
sum = 10 + 5 # 15
difference = 10 - 5 # 5
product = 10 * 5 # 50
quotient = 10 / 5 # 2
remainder = 10 % 3 # 1
Methods
Define reusable code with the def keyword:
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 user_signed_in?
redirect_to login_path
end
Loops
Repeat actions with loops:
# 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
Why Learn Ruby?
Ruby has been powering web applications for over two decades. Here 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 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.
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:
Check Your Ruby Version
ruby --version
You should see something like ruby 3.2.0 or newer.
Use a Code Editor
We recommend:
- VS Code with the Ruby extension
- RubyMine for full IDE features
- Sublime Text for a lightweight option
Join the Community
Help is available at:
- Ruby Forum (ruby-forum.com)
- Stack Overflow
- Reddit r/ruby
- Discord servers
What is Next?
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!