A different perspective on enumerables
Enumerables are collections of objects. Implict in that definition are two concepts: object references and collection ordering.
Ruby, like most other object oriented languages, deals with references to objects rather than the objects themselves directly. What does that mean?
What insight did you infer from this simplistic example? Yes, Batman is not a superhero!
But what else? Let me test your comprehension with an exercise. Figure out a way to change the value of b
to "batman"
so that printing the contents of superheroes
lists tom
and batman
.
String#sub!
a bit. You might have observed the bang (exclamation symbol: !
) in the sub!
method. It typically means 'dangerous' in Ruby. It is dangerous because it mutates the string in-place. Lets see how:
In the first example, we made the variable a
refer to a different object (the string "jerry"
). Thus the object_id, which is the id of the object that the variable refers to, changed.
However, when we did gsub!
on a
, the object_id remained the same, but the underlying object's value changed. This is what is referred to as 'mutating' an object. Let us see what impact this has when dealing with Enumerables.
This is why bang methods are dangerous - they mutate the objects in-place. Your Enumerable is only a collection of 'references' to objects. This is good - even if you change what a variable refers to, later in the code, the Enumerable will not change. After all, the Enumerable does not know anything about the variable. It stores only the objects referenced by those variables. But when you use a dangerous method, you are changing the value of every Enumerable that contains the object. You rarely want that.