Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What are Variables in Ruby?

In this blog post, we will learn about variables in Ruby. If you're new to programming, you might be wondering what variables are and why they are essential. Don't worry; we will explain everything step by step and provide examples to help you understand the concepts easily. So, let's get started!

What are Variables?

Think of variables as containers or storage boxes to keep your data. In programming, you often need to store and manipulate data, and variables make it possible by holding the data and giving it a name. The name you assign to a variable is called an "identifier."

Imagine you want to remember the name of your favorite book. In real life, you associate the book's name with the book itself. In programming, you can store the book's name in a variable and then use that variable whenever you want to remember the name of the book.

Here's an example:

favorite_book = "The Catcher in the Rye"
puts favorite_book

In this example, we created a variable named favorite_book and assigned it the value "The Catcher in the Rye." Then we used the puts method to display the value of the favorite_book variable, which is "The Catcher in the Rye."

Types of Variables in Ruby

Ruby has four types of variables: local variables, instance variables, class variables, and global variables. Let's look at each type in detail.

Local Variables

Local variables are the most common type of variables. They are only accessible within the scope they are defined in. The scope could be a method, a block, or a loop. Once the scope is exited, the variable is no longer accessible.

Local variables in Ruby start with a lowercase letter or an underscore (_).

Here's an example:

def display_name
  name = "John Doe"
  puts name
end

display_name

In this example, name is a local variable defined within the display_name method. When we call the display_name method, it prints "John Doe." However, name is only accessible within the display_name method, and not outside of it.

Instance Variables

Instance variables are associated with a specific object and can be accessed from any method within the object's class. They are useful for sharing data between different methods of the same object.

Instance variables in Ruby start with an "at" sign (@).

Here's an example:

class Person
  def set_name(name)
    @name = name
  end

  def display_name
    puts @name
  end
end

person = Person.new
person.set_name("John Doe")
person.display_name

In this example, @name is an instance variable defined within the Person class. We have two methods: set_name and display_name. The set_name method assigns a value to the @name variable, and the display_name method prints the value of the @name variable. Since @name is an instance variable, it can be accessed from both methods.

Class Variables

Class variables are associated with a class and can be shared between all objects of that class. They are useful for storing data that is common to all objects of a class.

Class variables in Ruby start with two "at" signs (@@).

Here's an example:

class Book
  @@total_books = 0

  def initialize
    @@total_books += 1
  end

  def self.total_books
    puts @@total_books
  end
end

book1 = Book.new
book2 = Book.new
book3 = Book.new

Book.total_books

In this example, @@total_books is a class variable defined within the Book class. The initialize method increments the value of @@total_books every time a new Book object is created. The total_books method, which is a class method, prints the value of the @@total_books variable. Since @@total_books is a class variable, it is shared among all objects of the Book class.

Global Variables

Global variables are accessible from any part of your program, including within classes and methods. They should be used sparingly, as they can lead to code that is hard to understand and maintain.

Global variables in Ruby start with a dollar sign ($).

Here's an example:

$greeting = "Hello, world!"

def display_greeting
  puts $greeting
end

class Greeter
  def greet
    puts $greeting
  end
end

display_greeting

greeter = Greeter.new
greeter.greet

In this example, $greeting is a global variable that can be accessed from both the display_greeting method and the greet method of the Greeter class.

Conclusion

In this blog post, we learned about variables in Ruby and their different types. We explored local variables, instance variables, class variables, and global variables with examples to help you understand their usage in Ruby programs. Now that you have a better understanding of variables in Ruby, you can start using them in your own projects to store and manipulate data more effectively. Good luck, and happy coding!