From: dblack@... Date: 2007-01-08T21:48:07+09:00 Subject: Re: OO way of local variable passing Hi -- On Mon, 8 Jan 2007, Krekna Mektek wrote: > Hi, > > What is the OO way to do this? > > > I've got a front-end script: > > front.rb which calls site.rb > > in site.rb there is a method1 and method2. > > method1 creates a directory (the name is based on some other info) and > this directoryname is needed in method2. > > > in front.rb I do this: > > I create the site object and then call: > site.method1 (new dir is created) > site.method2 (do some things in this directory) > > > So, the first method is like: > > def method1 > > ... > new_dir = [...magic code..] > ... > end > > > def method2 > ... > get_files = @basedir + new_dir > ... > end > > But, this new_dir variable is not known in method2. > > * Do I need to return this new_dir variable from method1, and call the > other method with new_dir as argument like site.method2(new_dir) ? > > * Do I have to make from new_dir a class variable? (the way I have it now) > > * Is there another way to do this? If I'm understanding the situation correctly it looks like you could use an instance variable: def method1 ... @new_dir = (whatever) ... end def method2 ... get_files = @basedir + @new_dir ... end That's the normal way of maintaining state for a particular object. David -- Q. What is THE Ruby book for Rails developers? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.com)