From: Rick DeNatale Date: 2008-09-18T23:33:54+09:00 Subject: Re: why one array continues to grow after repeated call ------=_Part_37845_21638232.1221748860256 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline On Thu, Sep 18, 2008 at 9:27 AM, Li Chen wrote: > Brian Candler wrote: > > Li Chen wrote: > >> I expect that once an array is set up it should be fixed regardless how > >> many times it is called. > > > > I'm not sure why you expect that. > > > > In your program, you first assign @b1 to an empty array. > > > > In method2, you loop around 4 times pushing '10' onto the end of the > > array. Therefore @b1 becomes, each time around the loop, > > [10] > > [10,10] > > [10,10,10] > > [10,10,10,10] > > How come this loop happan? I don't want a loop here. > Because that's what you coded: irb(main):008:1* def method2 irb(main):009:2> (method1.size).times{@b1<<10} irb(main):010:2> return @b1 irb(main):011:2> end $ qri Integer#times ---------------------------------------------------------- Integer#times int.times {|i| block } => int ------------------------------------------------------------------------ Iterates block int times, passing in values from zero to int - 1. It's not really clear what you are trying to do with your two methods, why not just initialize both instance variable in the intialize method? class A def initialize @a1 = [1,2,3,4] @b1 = [10].@a1.length # or more clearly just [10, 10, 10, 10] end def method1 @a1 end def method2 @b1 end end I'll leave discussion about using intention revealing method and variable names for another time. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/ ------=_Part_37845_21638232.1221748860256--