Mutability and Object Passing in Ruby

Ruby appears to use the object passing strategies; pass by value and pass by reference. Immutable objects such as integers, booleans, and nil seem to use pass by value. Mutable objects such as strings seems to use pass by reference.

Immutable values that are passed into a method will leave the original value unchanged by using a pass by value object passing strategy.

In the code snippet above, the method invocation square does not mutate the integer 2 , which is referenced by the local variable number. The local variable number is passed by value (a copy of the original object is made) to the method invocation square , binding the copy of the integer 2 to the variable num. The operator * on line 2 multiples the variable num with itself to return the integer 4. When we call the method invocation puts with the argument number on line 8, the integer 2 is outputted to the screen. We can see that the method invocation square did not mutate the integer 2 . Immutable values passed into a method will leave the original value unchanged by using the object passing strategy of pass by value.

Mutable objects seem to use a pass by reference object passing strategy. The method the object is passed to may or may not mutate the object.

In the code snippet above, the method does not mutate the string object passed to it. On line 5 the local variable string is initialized and assigned to the string object cat. The variable string is passed by reference to the method invocation double , binding string to the variable str. This means that both variables string and str point to the string object cat. On line 2 of the method invocation double, the operator * called with the argument of the integer 2 returns a new string object catcat. The return value of the method invocation double points to a different string object than the string cat. The local variable string still references the original string object cat . This code demonstrates that an object passed by reference to a non mutative method will not mutate the original object.

In this code snippet, the method mutates the string object passed to it. On line 5 the local variable string is initialized and assigned to the string object cat (has an object id of 180). The local variable string is passed by reference to the method invocation doubled! , binding string to str. Both variables, str and string reference the same string object cat. On line 2 the operator << is called on the variable str with an argument str. This type of concatenation is mutative, appending the object on the right of << to the object on the left. This results in the method invocation double! to return the string object catcat. Since the operator << is mutative, the method call puts with the argument string on line 12 results in the original string object mutated intocatcat. The object id of the string object cat on line 5 and the object id of the string object catcat on line 13 are both 180. This shows that an object passed by reference to a mutative method will result in the original object being mutated.