DEV Community

Andressa
Andressa

Posted on

Variables in Ruby

Quick index

  1. What are variables?
  2. Duping and Freezing Objects
  3. Types of variables

What are variables?

They are a pointer to a memory location. A reference. So when we assign it, it will actually hold the address in memory in which that object is stored.

for example:

address_holder = 'I am a String obj'
second_address_holder = address_holder

puts address_holder.object_id == second_address_holder.object_id #=> true
Enter fullscreen mode Exit fullscreen mode

If I change my string object, both variables should display the same:

pointer_to_location.upcase!

puts address_holder            #=> I AM A STRING OBJ
puts second_address_holder     #=> I AM A STRING OBJ
Enter fullscreen mode Exit fullscreen mode

References have a many-to-one relationship to their objects.

Immediate values
That behaviour is not always true. Some objects in ruby are stored in variables as immediate values: integers, symbols, true, false, and nill.

Any object that is represented as an immediate value is always exactly the same object, no matter how many variables it is assigned to. There is only one object 100, only one object false, and so on1.

Note that, if I reassign a variable, it will now point to another location. Take as an example:

a = 'first location'
b = a 

puts a.object_id    #=> 720
puts b.object_id    #=> 720

a = 'second location'

puts a.object_id    #=> 740
puts b.object_id    #=> 720
Enter fullscreen mode Exit fullscreen mode

Variable b in this case, is still pointing for the memory address a was pointing. Variable a is now pointing to a new location.

❗️ Ruby is not a typed variable language, which means we can assign a string to a variable now, and later on, we can assign an integer to it with not problem.

Duping and Freezing Objects

To protect objects from being changed there are three methods available:

freeze prevents from undergoing further change.
dup will duplicate the object. If you duplicate a frozen object, the duplicated one will not be frozen.
clone a lot like dup, but if you clone a frozen object, the clone will be also frozen.

note: if you freeze an array, you will be freezing the array itself, not the elements of it.

Types of variables

Constants Even though Ruby allows us to change those variables, they are used for variables that do not have a need to change or should not be changed.

MY_CONSTANT = 'I should not be changed through the application'
Enter fullscreen mode Exit fullscreen mode

Global variables They are accessible through the whole application and it is not recommended to use them if there is no need for it.

$global_variable = 'try avoiding me'
Enter fullscreen mode Exit fullscreen mode

Class variables They are accessible through the entire class and by instances of it.

@@class_variable = 'I can be modified inside my class or its instances.'
Enter fullscreen mode Exit fullscreen mode

Instance variables Those are only available inside the instance of the parent class.

@instance_variable = 'I don't follow strictly the scope rules.'
Enter fullscreen mode Exit fullscreen mode

local variables most common one.

local_variable = ''
Enter fullscreen mode Exit fullscreen mode

References


  1. David A. Black and Joseph Leo III. 2019. The Well-Grounded Rubyist. 3rd ed. 

Top comments (0)