Variables & Types
Reviewed & published by Brayan K
Ruby is a dynamic, beginner-friendly programming language, and variables are how you store and label the data your program works with — text, numbers, true/false, and "nothing".
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 assign variables, recognise Ruby's core types, check a value's class, and convert between strings and numbers.
What You'll Learn in This Lesson
- How to assign values to variables with =
- Ruby's core types: String, Integer, Float, Boolean, nil
- How to check any value's type with .class
- Why Ruby is dynamically typed and what that means
- Converting types with to_s, to_i, and to_f
- Naming conventions: snake_case and constants
1️⃣ Assigning Variables
You create a variable by writing a name, an =, and a value. Unlike many languages, you don't declare a type — Ruby reads the value and works out the type itself. Here are the five values you'll meet most: a String (text), an Integer (whole number), a Float (decimal), a Boolean (true/false), and nil (nothing).
# A variable is a name that points to a value. Use = to assign.
# Ruby figures out the type for you — no type declarations needed.
name = "Ada" # a String
age = 36 # an Integer
height = 1.7 # a Float (decimal number)
genius = true # a Boolean (true or false)
nickname = nil # nil means "no value at all"
puts name # -> Ada
puts age # -> 36
puts height # -> 1.7
puts genius # -> true
puts nickname.inspect # -> nil (inspect shows nil clearly)We used .inspect on nil because plain puts nil prints a blank line — .inspect shows the value clearly as nil.
2️⃣ Types and Converting Between Them
Every value knows its own type — ask with .class. Because Ruby is dynamically typed, a variable can point to a String now and a number later. When data crosses a boundary (like user input, which always arrives as text), you convert it with .to_i (to Integer), .to_f (to Float), or .to_s (to String).
# Every value has a class (its type). Ask with .class.
puts "hello".class # -> String
puts 7.class # -> Integer
puts 3.14.class # -> Float
puts true.class # -> TrueClass
puts nil.class # -> NilClass
# Ruby is dynamically typed: a variable can hold a String now...
thing = "text"
puts thing.class # -> String
# ...and a number later. The variable just re-points.
thing = 99
puts thing.class # -> Integer
# Convert between types with to_s, to_i, and to_f.
count = "10" # this is a String, not a number!
puts count.to_i + 5 # -> 15 (to_i turns "10" into 10)
puts 42.to_s + "!" # -> 42! (to_s turns 42 into "42")Your turn. Build a small player profile. Replace the three TODO values, keeping the right type for each, then run it.
# 🎯 YOUR TURN — replace each ___, then run with: ruby profile.rb
# 1) Store a username as a String
username = ___ # 👉 replace ___ with "neo"
# 2) Store a score as an Integer (no quotes!)
score = ___ # 👉 replace ___ with 250
# 3) Store whether the player is active as a Boolean
active = ___ # 👉 replace ___ with true
# Print a profile line using interpolation #{...}
puts "User: #{username} | Score: #{score} | Active: #{active}"
# Print the TYPE of each variable
puts "Types: #{username.class}, #{score.class}, #{active.class}"
# ✅ Expected output (for "neo", 250, true):
# User: neo | Score: 250 | Active: true
# Types: String, Integer, TrueClassMini-Challenge: A Unit Converter
Write a tiny miles-to-kilometres converter using a Float and interpolation. Run it with ruby convert.rb and check against the example.
# 🎯 MINI-CHALLENGE: a unit converter.
# Save as convert.rb and run with: ruby convert.rb
#
# 1. Store a distance in miles as a Float, e.g. 5.0
# 2. Multiply it by 1.60934 to get kilometres.
# 3. Print a sentence with both values using interpolation.
# 4. Also print the .class of your kilometres result.
#
# ✅ Example output:
# 5.0 miles is 8.0467 kilometres
# Result type: Float
# your code hereCommon Errors (and the fix)
- "String can't be coerced into Integer" — You tried to mix text and numbers, e.g. 5 + "3". Convert first: 5 + "3".to_i.
- Quotes around a number — "10" is text, not the number 10. Drop the quotes for arithmetic, or use .to_i.
- "undefined local variable or method" — You read a variable before assigning it, or mistyped its name. Assign it first, and check the spelling.
- Capitalised variable name — Names starting with a capital letter are constants in Ruby. Use snake_case for ordinary variables.
- Confusing = and == — = assigns a value; == compares two values. Mixing them up changes data when you meant to test it.
Pro Tips
- 💡 Use .inspect when debugging: it shows nil, quotes around strings, and the exact value, so you can tell "5" from 5.
- 💡 Parallel assignment: a, b = 1, 2 assigns both at once — handy and very Ruby.
- 💡 Freeze constants: mark unchanging values as constants (PI = 3.14159) so their intent is clear.
📋 Quick Reference — Variables & Types
| Concept | Ruby Syntax |
|---|---|
| Assign | age = 36 |
| Text / number / decimal | "hi" / 7 / 3.14 |
| Boolean / nothing | true / nil |
| Check type | value.class |
| To number / text | "5".to_i / 5.to_s |
| Constant | MAX = 100 |
🎉 Lesson Complete!
- ✅ Variables are named boxes you assign with =
- ✅ Ruby's core types: String, Integer, Float, Boolean, nil
- ✅ Check any value's type with .class
- ✅ Ruby is dynamically typed — a variable can change type
- ✅ Convert with to_i, to_f, and to_s
Practice quiz
Do you declare a variable's type in Ruby?
- Yes, always
- Only for numbers
- No — Ruby infers the type from the value
- Only for strings
Answer: No — Ruby infers the type from the value. Ruby is dynamically typed: you just assign a value and Ruby works out the type.
What is the class of 7?
- Integer
- Number
- Float
- Numeric
Answer: Integer. Whole numbers like 7 are of class Integer.
What is the class of 3.14?
- Integer
- Decimal
- Double
- Float
Answer: Float. Numbers with a decimal point are of class Float.
What does "10".to_i + 5 evaluate to?
- "105"
- 15
- an error
- "15"
Answer: 15. to_i converts the string "10" to the Integer 10, then 10 + 5 is 15.
What is the class of nil?
- NilClass
- NoClass
- FalseClass
- Object
Answer: NilClass. nil is the sole instance of NilClass.
Which values are 'falsy' in a Ruby condition?
- false, nil, 0, and ""
- Only false
- Only false and nil
- nil and 0
Answer: Only false and nil. Only false and nil are falsy; everything else (including 0 and "") is truthy.
What naming style do ordinary local variables use?
- camelCase
- snake_case
- PascalCase
- SCREAMING_SNAKE_CASE
Answer: snake_case. Local variables use snake_case — lowercase words joined by underscores.
A name that starts with a capital letter is treated by Ruby as a...
- local variable
- global variable
- method
- constant
Answer: constant. Ruby treats a name beginning with a capital letter as a constant.
What does a, b = 1, 2 do?
- A syntax error
- Parallel assignment: a becomes 1 and b becomes 2
- Assigns [1, 2] to a
- Assigns 1 to both
Answer: Parallel assignment: a becomes 1 and b becomes 2. Parallel assignment sets both variables at once: a = 1 and b = 2.
What is the difference between = and == in Ruby?
- They are the same
- == assigns, = compares
- = assigns a value; == compares two values
- Both compare
Answer: = assigns a value; == compares two values. = is assignment; == tests whether two values are equal.
Continue this course
- Previous: Introduction to Ruby
- Next: Operators & Expressions — Arithmetic, comparison, logical, the spaceship <=>, and parallel assignment
- Quick reference: Ruby cheat sheet › Basics
Frequently asked questions
Do I have to declare a variable's type in Ruby?
No. Ruby is dynamically typed, which means you never write the type. You simply assign a value — like name = "Ada" or age = 36 — and Ruby infers the type from the value. A variable can even hold a String at one point and an Integer later; the name just re-points to a new value. This makes Ruby quick to write, though it means type mistakes show up when the code runs rather than before.
What is the difference between nil and false?
nil means 'no value at all' — it is Ruby's way of saying 'nothing here yet', and its class is NilClass. false is a real boolean value meaning 'not true', and its class is FalseClass. They are different objects, but they share one important behaviour: in a condition, both nil and false are treated as 'falsy', while everything else (including 0 and empty strings) is treated as 'truthy'.
Why does "10" + 5 cause an error but "10".to_i + 5 work?
"10" with quotes is a String (text), not a number, so Ruby cannot add an Integer to it directly and raises a TypeError. The .to_i method converts the String "10" into the Integer 10, after which 10 + 5 is valid and gives 15. The reverse direction uses .to_s to turn a number into text. Converting between types like this is a constant part of working with user input, which always arrives as strings.
What naming style should I use for Ruby variables?
Ordinary (local) variables use snake_case — lowercase words joined by underscores, like total_price or user_name. Constants, which you don't intend to change, use SCREAMING_SNAKE_CASE like MAX_USERS. Following these conventions isn't just style: Ruby actually treats a name starting with a capital letter as a constant, so the casing carries meaning.