DEV Community

Cover image for Hitchhiker’s Guide to Ruby Variables
Sylwia Vargas
Sylwia Vargas

Posted on

Hitchhiker’s Guide to Ruby Variables

There are a few variable types in Ruby and that, I see, in the beginning confuses people a great deal. Welcome to my guide — I will use examples that refer to “The Hitchhiker’s Guide to the Galaxy” by Douglas Adams because it turned 42 this week. I’ll explain different variables starting from the most local in scope to the most global. Shall we begin?

1. Local variables

Local variables are declared by writing their name, then the = sign and their value, for instance:

favorite_quote = "Don't Panic."
Enter fullscreen mode Exit fullscreen mode

the scope of this variable is exactly where it is declared: if in a method, only the method will know about it. To see it for yourself, try this in your irb:

favorite_quote = "Don't Panic."

def what_is_the_fav_quote
  puts "My fav quote it #{favorite_quote}"
end

what_is_the_fav_quote
Enter fullscreen mode Exit fullscreen mode

You should get an error undefined local variable or method `favorite_quote' for main:Object - it's because the method has no knowledge of the variable that happened somewhere else.
There is one more thing to know about these variables: they can be overwritten. See here:


favorite_quote = "Don't Panic."
favorite_quote = 42

I didn't change only the value but also its datatype (from a string to an integer to).

2. Instance variables

When you instantiate an instance of a class, you can set an instance variable with a @ and its name:

`
class Person

attr_accessor :name

@@all = []

def initialize(name)
@name = name
@@all << self
end

def self.all
@@all
end
end
`
Now, let's create a new Person and immediately check this person's name:


Person.new("Marvin The Paranoid Android")
Person.all.first.name

How to decide if a variable should be instance or class? Well, if you think the data it holds is something specific to an individual object (e.g. every person has a name but it's a different name; or, every citizen/resident has an SSN but we wouldn't like others to know it), then you'd wanna make it an instance variable.

3. Class variables

Now, when I had to first conceptualize class variables, I imagined IRS that needs access to all people's names and SSNs. That would be the @@all in the above example. In your experience with beginner's Ruby you will most probably use class variables only in this very case: @@all.

4. Constants

Imagine that there is some data you want all functions in your class have an access to. For instance, you are making a game that's set in "Hitchhiker's Guide to the Galaxy" and you want to record forever what the answer to the Ultimate Question of Life, the Universe, and Everything is. You'd save it to a constant, declared in all caps:


ANSWER_TO_THE_ULTIMATE_QUESTION_OF_LIFE_THE_UNIVERSE_AND_EVERYTHING = 42
now. try overwriting it:
ANSWER_TO_THE_ULTIMATE_QUESTION_OF_LIFE_THE_UNIVERSE_AND_EVERYTHING = "puppies"

You'll get the errors:

warning: already initialized constant ANSWER_TO_THE_ULTIMATE_QUESTION_OF_LIFE_THE_UNIVERSE_AND_EVERYTHING
warning: previous definition of ANSWER_TO_THE_ULTIMATE_QUESTION_OF_LIFE_THE_UNIVERSE_AND_EVERYTHING was here

That's because constant never change. It's constant. You cannot overwrite it or change its datatype.

5. Global variables

Now, imagine a multi-file multi-class app. How can you make data travel easily? Enter global variables, declared with a $:


$book_data = {author: "Douglas Adams", year_published: 1978}

Now, you can use the data in the variable as much as you want across the app, provided that you put require 'path to the file' (e.g. require ./test.rb)

Top comments (0)