Ruby Gotcha #1

cristalcodes

Cristal

Posted on June 10, 2020

Ruby Gotcha #1

Hi dev friend!

Today I want to tell you about how I recently started The Complete Ruby on Rails Developer Course on Udemy, by Mashrur Hossain and Rob Percival. I'm aiming to deepen my the knowledge I obtained at Flatiron School, and strengthen the concepts that I feel comfortable with.

If you're looking to do the same, I'm happy to report that I will be taking notes as I progress through the course, and plan to upload them to Github. What I want to do right now, though, is share that first Ruby Gotcha! I've run into.

Ruby Gotcha #1: The difference between pass-by-reference and pass-by-value

In general, according to the instructor:

"In Ruby, if a variable is pointing to another variable, it's actually pointing the location in memory the variable is pointing to."

Here's an example:
Image of code snippet that shows a variable "name" assigned to the string "Cristal", a new variable "new-name" assigned to the variable "name", and a reassignment of the variable "name"

If you weren't aware of this gotcha, you might think that the variable "new_name" now points to "Joe". Here's what happens instead:

Image of code snippet that shows name evaluating to "Joe" and new_name evaluating to "Cristal"

The new_name variable is still pointing to that location in memory that holds the string "Cristal". Is your mind blown? Mine was!

It starts to get tricky once you actually start passing that variable around into methods. Let's take a look at that!

pass-by-reference

In Ruby, dynamic variables that can change in size are passed-by-reference, as in the location in memory. This means strings, arrays, and hashes.

By passing these variables around to methods using their reference, we are able to mutate them.

!!!IMPORTANT NOTE!!!

Remember that mutation is different from reassignment!
Here's what happens if you pass in a mutable variable to a method and then reassign it:

Alt Text

As you can see, name is still pointing to "Cristal" in memory.

But if we mutate the string, here's what happens:

Alt Text

"name" is pointing that spot in memory, but because we mutated the string, it is now permanently "Cristal Moz"

pass-by-value

Static variables- meaning that they don't change in size and are immutable- can be passed around as the actual value, instead of their reference. These include integers, fixnum, floats, and booleans. So let's look at an example...

Alt Text

As you can see, when we pass previous_age to the birthday method, it returns what we expect, but the value of previous age does not change. We cannot mutate an integer, fixnum, float, or boolean so that space in memory will always and forever point to that value.

Pretty neat and pretty confusing, right?

Maybe you're feeling like I did- you're thinking "WHAT?" In some of these scenarios, I thought the variable would be the same and then in others I thought it would be different. It helps to open up your terminal, get IRB going, and practice! Try passing different variable types around and seeing what happens!

Have a comment, question, suggestion?

Please reach out, I don't bite!

💖 💪 🙅 🚩
cristalcodes
Cristal

Posted on June 10, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related