A variable is a container used to store a value.
Here's a simple example of variables in Ruby. You can read the rest of the tutorial to learn more.
Example
name = "Alice"
age = 25
puts name
puts age
# Output:
# Alice
# 25
Here, name and age are variables that hold the values "Alice" and 25, respectively.
Create Ruby Variables
In Ruby, creating a variable is simple. You just choose a name and assign it a value using the = sign.
The syntax to create a Ruby variable is:
variable_name = value
For example,
price = 99
language = "Ruby"
Here,
priceis a variable that stores the number 99.languageis a variable that stores the text"Ruby".
Frequently Asked Questions
In Ruby, you don't need to mention the data type of a variable when creating it. Ruby figures it out automatically based on the value you assign. For example,
name = "Alice" # Ruby understands this is a string
age = 25 # Ruby knows this is a number
price = 99.99 # This is a decimal (float)
You don't need to say String, Integer, or Float — Ruby handles that.
Changing (Reassigning) Values
After creating a variable in Ruby, you can change its value anytime by assigning a new value to it. For example,
language = "Ruby"
puts language
language = "Python"
puts language
Output
Ruby Python
Here,
- At first, the variable
languagestores"Ruby". - Later, we reassign it to
"Python".
Types of Ruby Variables
Ruby has different variables based on where and how they are used.
The main types of variables are:
- Local Variables
- Instance Variables
- Class Variables
- Global Variables
Let's learn about them in detail.
1. Local Variables
Local variables are used within methods, blocks, or loops. They are only accessible from where they are defined and cannot be used outside that area.
For example,
def greet
_message = "Hello!" # local variable
puts _message
end
greet # Hello!
puts _message # Error: _message is not available outside the method
Here, _message = "Hello!" is a local variable defined inside the greet method. When greet is called, it prints "Hello!" because the variable exists within that method.
However, puts _message is placed outside the method, and since local variables only exist within the method where they are defined, this line causes an error.
Note: The local variables start with a lowercase letter or an underscore (_).
2. Instance Variables
Instance variables are used inside classes and belong to a specific instance of that class. They are accessible to all methods within the same instance, but not outside of it directly.
Instance variables always start with @.
Let's take a look at an example.
class Person
def set_name(name)
@name = name # instance variable
end
def show_name
puts @name # used in another method
end
end
p = Person.new
p.set_name("Alice")
p.show_name
Output
Alice
Here, @name is an instance variable. It's set in one method (set_name) and used in another (show_name).
Since both methods are part of the same instance (p), the @name variable works in both places.
However, attempting to access an instance variable directly using p.@name results in a syntax error, because instance variables are intended to be used only within the class.
puts p.@name # SyntaxError: unexpected '@'
3. Class Variables
A class variable is shared by all instances of a class. It starts with @@.
Let's take a look at an example.
class Animal
@@type = "Mammal" # class variable
def show_type
puts "Animal type: #{@@type}"
end
end
a1 = Animal.new
a2 = Animal.new
a1.show_type
a2.show_type
Output
Animal type: Mammal Animal type: Mammal
Here, @@type is a class variable — the same for all animals. Both a1 and a2 can access and print the same @@type value.
4. Global Variables
A global variable can be used anywhere in your Ruby program — inside methods, classes, or even outside of them. It always starts with a $ symbol.
Let's take a look at an example.
# global variable
$greeting = "Hello!"
def say_hello
puts $greeting
end
say_hello
puts $greeting
Output
Hello! Hello!
Here, $greeting is a global variable. It is defined outside the say_hello method, but can still be accessed from inside it.
Note: Try not to use global variables in big programs. They can make your code confusing and cause unexpected bugs.
Quick Summary
Let's wrap up the different types of variables in Ruby with a clear and concise overview:
| Variable Type | Prefix | Scope |
|---|---|---|
| Local Variable | lowercase letter or _ |
Only inside the method/block where it is defined |
| Instance Variable | @ |
Accessible to all methods within the same instance of the class |
| Class Variable | @@ |
Shared across all instances of a class |
| Global Variable | $ |
Accessible from anywhere in the program |