Ruby is a straightforward language. It’s dynamic and the syntax is amongst the easiest to understand language. In fact, Ruby is associated with love and joy! Awesome, isn’t it?

Today, we’re going to play with Ruby variable. Ruby is a dynamic language. Declaring variable doesn’t need much effort. Furthermore, every Ruby variable has a built-in method called object_id. Everything in Ruby is an object. And each object has a unique identifier called object_id that represent pointer on low-level machine code. You can dig it here and here.

Let’s play with this code

Let’s explore it! We have 3 variables foo, bar, and baz. From the object_id we know that bar and baz are actually the same object with a different name. It’s quite expected since we do assignment baz = bar. However, what you might not expect is that it’s shared a reference to a single object! Since it’s a shared reference, we can do something like this.

Notice that by changing baz, we also change bar. Vice versa. From this object_id, we know that Ruby variable is passed by reference. Ruby variable might like Javascript variable, it looks like pass by value. However, Ruby internal mechanism for passing variable is quite different since all variable in Ruby is an object and every object own its reference object_id.

Reassignment

Since we know that Ruby is using pass by reference, let’s play with reassignment! Previously, we declare baz = bar. What happens if we change bar? What will reassignment do to object_id? Let’s do it!

Reassignment didn’t change baz at all! bar got new object_id and baz object_id didn’t change. What happens is when we do reassignment, the referenced object didn’t change, instead it creates a new object for the reassigned variable.

Fixnum

Sometimes, we use increment, right? What about passing strategy when we do increment? Let’s find out!

In Ruby, numbers and boolean values are immutable. Once we create an immutable object, we cannot change it. Align with what we’ve learned about reassignment when we do increment in Ruby, we don’t actually change the object. Instead, we assign the incremented variable to another object.

What’s more interested in Ruby is that immutable object always has the same object id.

What about mutability in Ruby?

As we discover before, everything in Ruby is an object, including variable! For the object in Ruby itself, there are 2 kinds of it: mutable object and immutable object. The mutable object is object that can be changed.

By default, all objects in Ruby are mutable. However, some objects are immutable. Those immutable objects are NilClass, Integer, Float, Symbol, Boolean, and several other objects. Ruby also can make any object immutable by invoking .freeze to an object.

However, the frozen object still has different object_id. It only strips out mutable method from the available method on a variable.

Mutable method

Ruby object has built-ins method called mutable method. Basically, mutable methods are a method that can be called to mutate the object. In this article, we learn that << and upcase! are mutable method. There are several other mutable methods available like [], concat, etc.

Conclusion

We learned that all object in Ruby has an identifier, it contains a reference to that object in low-level machine code. Ruby use pass by reference. However, for the immutable object, Ruby use pass by value.

Using the fundamental of pass by reference, we can do a trick on modifying variable, especially if the variable is a large hash.

Using fundamental of pass by reference and mutability, we also should be aware when we call a mutable method on any objects, especially when those objects are unfrozen.


This post also available on my personal blog. Feel free to visit :smile: