From: Cristi BALAN Date: 2005-07-29T19:18:11+09:00 Subject: Re: [newbie] make local var visible On 7/29/05, "Pe�a, Botp" wrote: > Hi All, > > I may not be feeling well today, so pardon me pls. > > I am modifying/cleaning many scripts here (most of them just a page long, > two at most, and most of them coming fr perl/bash origin). > > I encountered a wall though. > > Consider, > > >cat test.rb > > #sample script---------------->8 > foo=["a","b","c"] > > def mfoo > foo.each do |f| > p f > end > end > > # go-go-go! > mfoo > #end sample script------------>8 > > i just enclosed the some commands with def mfoo / end to make a new method > mfoo and make my script cleaner, and besides, i may be calling mfoo again. > > running the above, i get: > > >test.rb > > >test.rb:4:in `mfoo': undefined local variable or method `foo' > from test.rb:9 > > > So how can i force foo visible to mfoo()? > > Thanks in advance. > -botp > > > I usually use globals... *duck* but, the following also works since, it think?, all ruby code is inside class Object ... end @foo = [1, 2, 3] class Object attr_accessor :foo end def mfoo puts foo.inspect end mfoo hope this helps -- Cristi BALAN