Question about Array.push

Started by Ryex, February 06, 2010, 05:44:04 pm

Previous topic - Next topic

Ryex


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?
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

Fantasist

1. Why didn't you just try that? :P
2. As far as I know, yes, they refer to the same object.
Do you like ambient/electronic music? Then you should promote a talented artist! Help out here. (I'm serious. Just listen to his work at least!)


The best of freeware reviews: Gizmo's Freeware Reviews




Blizzard

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.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Ryex

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
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

Fantasist

Yes, that's how it works. I discovered this when skimming through the STCMS (see what Blizz did with the windows)
Do you like ambient/electronic music? Then you should promote a talented artist! Help out here. (I'm serious. Just listen to his work at least!)


The best of freeware reviews: Gizmo's Freeware Reviews




Blizzard

Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.