Ruby Variables

A variable is a container (storage area) to hold data.

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

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,

  • price is a variable that stores the number 99.
  • language is a variable that stores the text "Ruby".

Frequently Asked Question

Do we need to declare the data type when creating variables?

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 an integer 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, you can change its value anytime by assigning a new value to it. For example,

language = "Ruby"
puts language     

# Change the value of the variable
language = "Python"
puts language  

Output

Ruby
Python

Here,

  • At first, the variable language stores "Ruby".
  • Later, we reassigned 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
  # Local variable
  _message = "Hello!"
  puts _message
end

# Call the greet method
# Prints 'Hello!' to the screen
greet

# Error: _message is not available outside the method
puts _message

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. Since local variables only exist within the method where they are defined, this line causes an error.

Note: 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 @. For example,

class Person

  def set_name(name)
    # Create and initialize an instance variable
    @name = name   
  end

  def show_name
    # Print the instance variable
    puts @name
  end
end

# Create an instance of the Person class
p = Person.new

p.set_name("Alice")
p.show_name

Output

Alice

Here, @name is an instance variable. It's created and initialized 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 @@. For example,

class Animal
  @@type = "Mammal"   # Class variable

  def show_type
    puts "Animal type: #{@@type}"
  end
end

# Create an instance of the Animal class
a1 = Animal.new

# Create another instance of Animal
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 instances of Animal type. Thus, 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. For 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 get a quick overview of the different types of variables in Ruby:

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.
Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges