Ruby Gotcha #1
Tags: ruby
Published: 2020-06-10 23:14:27

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"](https://dev-to-uploads.s3.amazonaws.com/i/zc714t1s9wt68oo44w2m.png)

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"](https://dev-to-uploads.s3.amazonaws.com/i/81dlfr6wgw0qyr1c10q3.png)

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. 

<center>!!!IMPORTANT NOTE!!!</center>
Remember that mutation is different from reassignment! 
Here's what happens if you pass in a <em>mutable</em> variable to a method and then reassign it: 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/gggzywzqtuwmades7zz8.png)

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

But if we <em>mutate</em> the string, here's what happens:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/wm9tao1d3xzp0mczqa0m.png)

"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...<br>

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ydz9a0ufs8nfjp9eprvh.png)

As you can see, when we pass previous_age to the birthday method, it returns what we expect, <em>but the value of previous age does not change</em>. 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!