Chaos Project

RPG Maker => RPG Maker Scripts => Topic started by: Ryex on February 06, 2010, 05:44:04 pm

Title: Question about Array.push
Post by: Ryex on February 06, 2010, 05:44:04 pm

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?
Title: Re: Question about Array.push
Post by: Fantasist on February 07, 2010, 07:05:16 am
1. Why didn't you just try that? :P
2. As far as I know, yes, they refer to the same object.
Title: Re: Question about Array.push
Post by: Blizzard on February 08, 2010, 04:46:05 am
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.
Title: Re: Question about Array.push
Post by: Ryex on February 13, 2010, 02:49:52 pm
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
Title: Re: Question about Array.push
Post by: Fantasist on February 13, 2010, 03:01:04 pm
Yes, that's how it works. I discovered this when skimming through the STCMS (see what Blizz did with the windows)
Title: Re: Question about Array.push
Post by: Blizzard on February 14, 2010, 05:59:18 am
What she said. I mean he. :V