Hi all,

I write a script for studying purpose:
create a new array(b1) based on an existing one (a1).

I expect that once an array is set up it should be fixed regardless how
many times it is called. But I find array b1 continues to grow if I call
it repeatedly? I can't find an explantion for that. Any idea?

Thank you very much in advance,

Li



C:\Users\Alex>irb
irb(main):001:0> class A
irb(main):002:1>   def  initialize
irb(main):003:2>     @a1=[]
irb(main):004:2>     @b1=[]
irb(main):005:2>     method1
irb(main):006:2>   end
irb(main):007:1>
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
irb(main):012:1>
irb(main):013:1*
irb(main):014:1*   def method1
irb(main):015:2>    @a1=[1,2,3,4]
irb(main):016:2>   return @a1
irb(main):017:2>  end
irb(main):018:1>
irb(main):019:1* end
=> nil
irb(main):020:0> ob=A.new
=> #<A:0x2f9f7c @b1=[], @a1=[1, 2, 3, 4]>
irb(main):021:0> ob.method2
=> [10, 10, 10, 10]
irb(main):022:0> ob.method2
=> [10, 10, 10, 10, 10, 10, 10, 10]
irb(main):023:0> ob.method2
=> [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
irb(main):024:0> ob.method1
=> [1, 2, 3, 4]
irb(main):025:0> ob.method2
=> [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
irb(main):026:0> exit
-- 
Posted via http://www.ruby-forum.com/.