Coming from a c++ background I’m curious about object assignment in Ruby. What considerations (if any) should be made for the following object assignments:
class MyClass
attr_accessor :a, :b
def initialize(a, b)
@a = a
@b = b
end
def some_method
puts "#{self.a} #{self.b}"
end
end
m = MyClass.new("first", "last")
n = MyClass.new("pizza", "hello")
q = n
q.some_method
If you’re familiar with C++, then you might want to consider every variable in Ruby, instance or otherwise, as a reference to another object. Since everything in Ruby is an object, even
nil, which is of type NilClass, this holds true under every circumstance.To determine which object you’re referencing, you can use the
object_idmethod to differentiate. That’s similar to converting to a pointer using&in C++.Consider this:
Since
ais a reference to that string, andbis a copy ofa, then they are actually different references to the same object.This is important because operations that modify an object affect all references to that equally:
However, operations that create a new object will not modify all copies:
Many methods in Ruby are identified by a
!to distinguish in-place versus new-copy versions, but this is not always the case, so you have to be familiar with each method in order to be sure.Generally an assignment will replace an old reference with a new one, so as a rule of thumb,
=will replace old references. This applies to+=,-=,||=,&&=and so on.Edit: Updated based on Phrogz’s comment about using
ObjectSpace._id2ref(object_id)to convert an object identifier into an object.