class Thing
attr_accessor :bool
def init
@bool = false
end
end
class A
attr_accessor :array
attr_accessor :object
def init
@object = Thing.new
@array = []
@array.push(@object)
end
end
begin
$item = A.new
end
What I want to know is if the object added to an array through push is still the same object
ie in the code above, if the call
$item.object.bool = true was called
would
$item.array[0].bool == true?
and vise versa
if
$item.array[0].bool = true
was called would
$item.object.bool == true?
1. Why didn't you just try that? :P
2. As far as I know, yes, they refer to the same object.
There are two different types of objects: value objects and reference objects. Value objects are always copies and changing the copy will not modify the original. Those would be numbers, bool, nil, false and true. I think those are all. The others are reference objects and if you store them somewhere else you can modify the original, you always modify the original.
And to answer you question, yes. As I said, only a few things are value objects, the rest are all reference.
so to be clear
@array = []
@thing - Thing.new
@thing.x = 3
@array.push(@thing)
@array[0].x #> 3
@thing.x = 100
@array[0].x #> 100
Yes, that's how it works. I discovered this when skimming through the STCMS (see what Blizz did with the windows)
What she said. I mean he. :V