Variables in Ruby
Tags: beginners, codenewbie, ruby
Published: 2022-06-18 10:40:50

**_Quick index_**
1. [What are variables?](#what-are-variables?)
2. [Duping and Freezing Objects] (#duping-and-freezing-objects) 
3. [Types of variables](#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: 

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

puts address_holder.object_id == second_address_holder.object_id #=> true
```

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

```ruby
pointer_to_location.upcase!

puts address_holder            #=> I AM A STRING OBJ
puts second_address_holder     #=> I AM A STRING OBJ
```
References have a many-to-one relationship to their objects.

<u>**_Immediate values_**</u>
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 on[^1].

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

```ruby 
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
```

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. 

<u>note</u>: 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. 
```ruby
MY_CONSTANT = 'I should not be changed through the application'
```

`Global variables` They are accessible through the whole application and it is not recommended to use them if there is no need for it. 
```ruby
$global_variable = 'try avoiding me'
```

`Class variables` They are accessible through the entire class and by instances of it. 
```ruby 
@@class_variable = 'I can be modified inside my class or its instances.'
```

`Instance variables` Those are only available inside the instance of the parent class.
```ruby 
@instance_variable = 'I don't follow strictly the scope rules.'
```

`local variables` most common one. 
```ruby 
local_variable = ''
```


_____
## References
+ [Variables](https://launchschool.com/books/ruby/read/variables)

+ [The Well-Grounded Rubyist](https://www.oreilly.com/library/view/the-well-grounded-rubyist/9781617295218/)

[^1]: David A. Black and Joseph Leo III. 2019. _The Well-Grounded Rubyist_. 3rd ed.