Ruby on Rails Zero to Advanced #1: Ruby Basics
Welcome to Ruby on Rails Zero to Advanced, a 10-part series that takes you from your first line of Ruby to a deployable Rails application. Rails is famous for letting small teams ship fast, but a lot of that "magic" is simply good Ruby. If you understand Ruby first, Rails stops looking magical and starts looking obvious.
In this first part we focus only on the language: running code in irb, variables, numbers, strings, printing with puts and p, and symbols. Every example shows its output so you can compare it with your own terminal.
Running Ruby with irb
Make sure Ruby 3.x is installed (we will cover installing Rails itself in part 4). Check your version, then start irb, the Interactive Ruby shell. It evaluates each line you type and prints the result after =>.
ruby -v
irb
Output:
ruby 3.4.1 (2024-12-25 revision 48d4efcb85) +PRISM [arm64-darwin24]
irb(main):001>
Your version number may differ slightly; anything from 3.2 upward works for this series. Now type some expressions:
1 + 2
10 / 3
10 / 3.0
10 % 3
2 ** 10
1_000_000
Output:
=> 3
=> 3
=> 3.3333333333333335
=> 1
=> 1024
=> 1000000
Notice that 10 / 3 returns 3: dividing two integers gives an integer. Use a float (3.0) when you need decimals. Underscores in numbers are only for readability. Type exit to leave irb.
Variables and dynamic typing
Ruby variables do not need a type declaration. You just assign a value. By convention, variable names use snake_case.
name = "Budi"
age = 25
is_admin = false
price = 49.99
name.class
age.class
is_admin.class
price.class
nil.class
Output:
=> "Budi"
=> 25
=> false
=> 49.99
=> String
=> Integer
=> FalseClass
=> Float
=> NilClass
Everything in Ruby is an object, even numbers and nil, which is why you can call .class on anything. nil means "no value" and, together with false, is the only falsy value in Ruby. Zero and empty strings are truthy, which surprises developers coming from JavaScript or PHP.
Ruby is dynamically typed but strongly typed, so it will not silently mix types:
"5" + 5
"5".to_i + 5
5.to_s + "5"
Output:
(irb):1:in 'String#+': no implicit conversion of Integer into String (TypeError)
=> 10
=> "55"
Strings and interpolation
Strings can use double or single quotes. The difference matters: only double quotes support interpolation with #{} and escape sequences like \n.
name = "Siti"
"Hello, #{name}!"
'Hello, #{name}!'
"Total: #{2 * 21}"
Output:
=> "Siti"
=> "Hello, Siti!"
=> "Hello, \#{name}!"
=> "Total: 42"
Strings come with many useful methods. Method names ending in ? return a boolean, and names ending in ! usually modify the object in place.
city = "jakarta"
city.upcase
city.capitalize
city.length
city.include?("kart")
city.reverse
"Rails" * 3
" padded ".strip
"budi,siti,andi".split(",")
Output:
=> "jakarta"
=> "JAKARTA"
=> "Jakarta"
=> 7
=> true
=> "atrakaj"
=> "RailsRailsRails"
=> "padded"
=> ["budi", "siti", "andi"]
Compare the normal and "bang" versions:
city = "jakarta"
city.upcase
city
city.upcase!
city
Output:
=> "jakarta"
=> "JAKARTA"
=> "jakarta"
=> "JAKARTA"
=> "JAKARTA"
upcase returns a new string and leaves city unchanged, while upcase! changes city itself.
Printing: puts, print and p
In a script (not irb) you need to print things explicitly. Save this as hello.rb and run it with ruby hello.rb:
name = "Budi"
puts "Hello, #{name}"
print "No newline here. "
print "Same line.\n"
p name
p 42
puts nil
p nil
puts [1, 2, 3]
p [1, 2, 3]
Output:
Hello, Budi
No newline here. Same line.
"Budi"
42
nil
1
2
3
[1, 2, 3]
putsprints a human-friendly version and adds a newline. It returnsnil.printis likeputswithout the newline.pprints the developer-friendlyinspectversion (strings keep their quotes,nilis visible) and returns the value. It is perfect for debugging.
In Rails you will rarely use puts in application code; you will use Rails.logger instead. But p remains a great quick debugging tool.
Symbols
A symbol looks like :name. Think of it as a lightweight, immutable label. The same symbol is always the exact same object in memory, while two equal strings are usually two different objects.
:status.class
:status.object_id == :status.object_id
"status".equal?("status")
"active".to_sym
:active.to_s
Output:
=> Symbol
=> true
=> false
=> :active
=> "active"
Symbols are everywhere in Rails: hash keys ({ name: "Budi" }), method options (has_many :posts), route definitions (resources :articles) and validations (validates :title, presence: true). Rule of thumb: use a string for data (text the user sees or types) and a symbol for names and identifiers in your code.
Common mistakes
- Interpolating inside single quotes.
'Hi #{name}'prints the braces literally. Use double quotes. - Expecting 0 or "" to be false. In Ruby only
nilandfalseare falsy. Check emptiness with.empty?or, in Rails,.blank?. - Integer division surprises.
7 / 2is3. Use7 / 2.0or7.fdiv(2)for3.5. - Using camelCase. Ruby code uses
snake_casefor variables and methods, andCamelCaseonly for class and module names. - Debugging with puts.
puts nilprints an empty line, hiding the problem.pshowsnilclearly.
Exercise
Create profile.rb that stores a first name, a city and a birth year in variables, then prints one sentence such as "Siti from Bandung is 28 years old." Calculate the age from the year with simple arithmetic, print the city in uppercase, and finally print the sentence length using p.
first_name = "Siti"
city = "Bandung"
birth_year = 1998
age = 2026 - birth_year
sentence = "#{first_name} from #{city.upcase} is #{age} years old."
puts sentence
p sentence.length
Output:
Siti from BANDUNG is 28 years old.
34
Summary
You can now run Ruby in irb and as a script, store values in variables, work with numbers and strings, interpolate with #{}, choose between puts and p, and you know why Rails code is full of symbols. In part 2 we will look at arrays, hashes, blocks and methods, the tools you will use in literally every Rails file.
Related articles
PostgreSQL Zero to Advanced #1: Introduction & Setup
Start the series by understanding what a relational database is, installing PostgreSQL locally, and getting comfortable with psql commands like \l, \c and \dt.
PHP Zero to Advanced #1: Introduction & Setup
Start your PHP journey: what PHP is, how to install PHP 8.2+, run the built-in development server, and write your very first script with echo.
Python Zero to Advanced #1: Introduction & Setup
Start your Python journey: what Python is, how to install it, how to use the REPL, print your first output, and run .py files from the terminal.