From: Brian Candler Date: 2008-09-21T18:36:34+09:00 Subject: Re: why one array continues to grow after repeated call Li Chen wrote: > The script in the OP is a simple version of another more lenghy one. > > @a1 comprise all the file names from a folder. > @b1 comprise all the file names from another folder. > the construction of @b1 is dependent on @a1. > after initialize I want to check the status of @a1 and @b1. > out of curiossity I call both method1 and method2 more than once. So if the idea is for method1 to initialize @a1, and for method2 to initialize @b1, then do it like this: def initialize method1 method2 end def method1 @a1 = [1,2,3,4] end def method2 @b1 = [] @a1.size.times { @b1 << 10 } @b1 end > I think I can retrieve the status of @a1 and @b1 by > writing accessor/getter. This way I would not mess up with the method > call. Sure: def a1 @a1 end def b1 @b1 end or more simply: attr_reader :a1, :b1 If you want, you can make the accessor method also perform the initialization. Here's a common pattern: def a1 @a1 ||= method1 end def b1 @b1 ||= method2 end B. -- Posted via http://www.ruby-forum.com/.