Forum: Ruby problem with array ... object inside disappear

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9e8274f1e5340cc2efe0e4bc2f83121f?d=identicon&s=25 MR Damien (mrdamien)
on 2008-12-09 14:59
Hi dudes,

I am becoming crazy with an array behaviour.

Here is the stuff.

I have an objet PackageList that keep an internal list of packages
inside an array.

class PackageList
  def initialize
    @list = Array.new
  end

  def refresh
    p @list    # show list first time
    @list.each do |item|
      p item.name # show list second time
    end
  end

end

The problem here is that when I show the list for the first time I
didn't get the same result than the second time depiste I didn't change
anything in my list.

extract from first time
[#<Package:0x435e504 @maj_number=3, @min_number=2, @name="test1">,
#<Package:0x435d604 @maj_number=3, @min_number=1, @name="test2">,
#<Package:0x435cb64 @maj_number=1, @min_number=2, @name="test3">, ...]

extract from second time
"test1"
"test3"
...

The "test2" object disappeared depiste I didn't change anything. Now if
I call the method for 'each' latter in the software, the "test2" object
appears again.

I can't find why the list is changing even if I didn't touch anything.
9e8274f1e5340cc2efe0e4bc2f83121f?d=identicon&s=25 MR Damien (mrdamien)
on 2008-12-09 15:25
OK, the problem seems to be with rake tasks. Inside one of them, there
is some delay between what it prints and the changes made to the list.

Weird.
9e8274f1e5340cc2efe0e4bc2f83121f?d=identicon&s=25 MR Damien (mrdamien)
on 2008-12-09 15:42
MR Damien wrote:
> OK, the problem seems to be with rake tasks. Inside one of them, there
> is some delay between what it prints and the changes made to the list.
>
> Weird.

Finally, I got it. I was modifying the array where I was iterating from
that was causing weird issues.

I use 2 array now, problem solved.
9e8274f1e5340cc2efe0e4bc2f83121f?d=identicon&s=25 MR Damien (mrdamien)
on 2008-12-09 16:24
MR Damien wrote:
> MR Damien wrote:
>> OK, the problem seems to be with rake tasks. Inside one of them, there
>> is some delay between what it prints and the changes made to the list.
>>
>> Weird.
>
> Finally, I got it. I was modifying the array where I was iterating from
> that was causing weird issues.
>
> I use 2 array now, problem solved.

And Yes, I like to speak alone !!! :-)
This topic is locked and can not be replied to.