# \[newbie\] make local var visible

**URL:** <https://rubytalk.org/t/newbie-make-local-var-visible/19802>\
**Category:** ruby-talk\
**Created:** [29 July 2005 09:55 UTC](https://rubytalk.org/t/newbie-make-local-var-visible/19802 "2005-07-29T09:55:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![\_Pena\_Botp1](https://avatars.discourse-cdn.com/v4/letter/_/94ad74/32.png) [@\_Pena\_Botp1](https://rubytalk.org/u/_Pena_Botp1)\
**Post date:** [29 July 2005 09:55 UTC](https://rubytalk.org/t/newbie-make-local-var-visible/19802/1 "2005-07-29T09:55:31Z")

</div>

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  
&nbsp;&nbsp;&nbsp;&nbsp;foo.each do |f|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p f  
&nbsp;&nbsp;&nbsp;&nbsp;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'

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from test.rb:9

So how can i force foo visible to mfoo()?

Thanks in advance.  
-botp

---

<div class="post-metadata">

**Author:** ![Brian\_Schroder1](https://avatars.discourse-cdn.com/v4/letter/b/2bfe46/32.png) [@Brian\_Schroder1](https://rubytalk.org/u/Brian_Schroder1)\
**Post date:** [29 July 2005 10:16 UTC](https://rubytalk.org/t/newbie-make-local-var-visible/19802/2 "2005-07-29T10:16:23Z")

</div>

If you really want this, you could prepend an @ to the foo

$ cat mfoo.rb  
@foo = [1,2,3]  
foo = [5,6,7]

def afoo  
&nbsp;&nbsp;@foo  
end

def mfoo  
&nbsp;&nbsp;foo  
end

p afoo  
p mfoo  
$ ruby mfoo.rb  
[1, 2, 3]  
mfoo.rb:9:in `mfoo': undefined local variable or method `foo' for  
#\<Object:0xb7ef79c0 @foo=[1, 2, 3]\> (NameError)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from mfoo.rb:13

regards,

Brian

> **···**
>
> On 29/07/05, "Peña, Botp" \<botp@delmonte-phil.com\> 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  
> > &nbsp;&nbsp;&nbsp;&nbsp;foo.each do |f|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p f  
> > &nbsp;&nbsp;&nbsp;&nbsp;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'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from test.rb:9
> > 
> > So how can i force foo visible to mfoo()?
> > 
> > Thanks in advance.  
> > -botp
> 
> --  
> [http://ruby.brian-schroeder.de/](http://ruby.brian-schroeder.de/)
> 
> Stringed instrument chords: [http://chordlist.brian-schroeder.de/](http://chordlist.brian-schroeder.de/)

---

<div class="post-metadata">

**Author:** ![Cristi\_BALAN](https://avatars.discourse-cdn.com/v4/letter/c/c2a13f/32.png) [@Cristi\_BALAN](https://rubytalk.org/u/Cristi_BALAN)\
**Post date:** [29 July 2005 10:18 UTC](https://rubytalk.org/t/newbie-make-local-var-visible/19802/3 "2005-07-29T10:18:11Z")

</div>

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  
&nbsp;&nbsp;attr\_accessor :foo  
end

def mfoo  
&nbsp;&nbsp;puts foo.inspect  
end

mfoo

hope this helps

> **···**
>
> On 7/29/05, "Peña, Botp" \<botp@delmonte-phil.com\> 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  
> > &nbsp;&nbsp;&nbsp;foo.each do |f|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p f  
> > &nbsp;&nbsp;&nbsp;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'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from test.rb:9
> > 
> > So how can i force foo visible to mfoo()?
> > 
> > Thanks in advance.  
> > -botp
> 
> --  
> Cristi BALAN

---

<div class="post-metadata">

**Author:** ![wannes](https://avatars.discourse-cdn.com/v4/letter/w/e9a140/32.png) [@wannes](https://rubytalk.org/u/wannes)\
**Post date:** [29 July 2005 10:22 UTC](https://rubytalk.org/t/newbie-make-local-var-visible/19802/4 "2005-07-29T10:22:05Z")

</div>

def mfoo ... end creates a new scope (or how would you say that in  
proper naming), and thus forgets about local variables defined  
elsewhere, you can't access foo from within the method.

I think you have 2 options here (maybe more, but these are the ones I  
can come up with as a newbie myself):  
1) pass foo as an argument.  
&nbsp;&nbsp;def mfoo(foo) ... end  
&nbsp;&nbsp;mfoo(foo)  
2) make foo an instance-variable  
&nbsp;&nbsp;@foo = [..]  
&nbsp;&nbsp;def mfoo  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@foo.each...  
&nbsp;&nbsp;end

grtz,  
wannes

> **···**
>
> On 29/07/05, "Peña, Botp" \<botp@delmonte-phil.com\> wrote:
> 
> > #sample script----------------\>8  
> > foo=["a","b","c"]
> > 
> > def mfoo  
> > &nbsp;&nbsp;&nbsp;&nbsp;foo.each do |f|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p f  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > # go-go-go!  
> > mfoo  
> > #end sample script------------\>8  
> > \>test.rb:4:in `mfoo': undefined local variable or method `foo'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from test.rb:9  
> > So how can i force foo visible to mfoo()?

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [29 July 2005 12:21 UTC](https://rubytalk.org/t/newbie-make-local-var-visible/19802/5 "2005-07-29T12:21:01Z")

</div>

You could do:

&nbsp;&nbsp;Foo=["a","b","c"]

&nbsp;&nbsp;def mfoo  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Foo.each do |f|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p f  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

&nbsp;&nbsp;# go-go-go!  
&nbsp;&nbsp;mfoo

Yet I tend to agree. Even in classes, it just seems so much more  
elegant:

&nbsp;&nbsp;class Bar

&nbsp;&nbsp;&nbsp;&nbsp;foo = ["a","b","c"] # attribute

&nbsp;&nbsp;&nbsp;&nbsp;def mfoo  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foo.each do |f|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p f  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;end

But the only way to do that woud be:

1) Irregular attr syntax.

&nbsp;&nbsp;&nbsp;&nbsp;attr foo = ["a","b","c"]

2) Alternate assignment operator.

&nbsp;&nbsp;&nbsp;&nbsp;foo := ["a","b","c"]

3) Prefixed local vars.

&nbsp;&nbsp;%local\_var = 'what have you'

I myself have suggested before that

&nbsp;&nbsp;foo = 10

be essentially equivalent to

&nbsp;&nbsp;def foo ; 10 ; end

And that constants and methods share the same namespace.

T.

---

<div class="post-metadata">

**Author:** ![David\_Vallner](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@David\_Vallner](https://rubytalk.org/u/David_Vallner)\
**Post date:** [29 July 2005 20:35 UTC](https://rubytalk.org/t/newbie-make-local-var-visible/19802/6 "2005-07-29T20:35:15Z")

</div>

IMO, if you really want clean in the code readability sense, stop trying to transliterate code between languages, and go the usual way:

foo = ['something', 'or', 'other']

def mfoo(arg)  
&nbsp;&nbsp;# do stuff with a copy of arg.  
&nbsp;&nbsp;return arg  
end

foo = mfoo(foo)

If you really want foo clobbered, I'd possibly either add an Array#mfoo! method, or add it as a singleton to foo. Yes, I'm -that- evil 😉 And resentful of clobbering objects...

David

---

<div class="post-metadata">

**Author:** ![Daniel\_Brockman](https://avatars.discourse-cdn.com/v4/letter/d/ed8c4c/32.png) [@Daniel\_Brockman](https://rubytalk.org/u/Daniel_Brockman)\
**Post date:** [29 July 2005 11:38 UTC](https://rubytalk.org/t/newbie-make-local-var-visible/19802/7 "2005-07-29T11:38:10Z")

</div>

wannes \<oeaniz@gmail.com\> writes:

> def mfoo ... end creates a new scope (or how would you say  
> that in proper naming),

You'd say it doesn't create closures.

> and thus forgets about local variables defined elsewhere,  
> you can't access foo from within the method.
> 
> I think you have 2 options here (maybe more, but these are  
> the ones I can come up with as a newbie myself):
> 
> 1) pass foo as an argument.  
> &nbsp;&nbsp;def mfoo(foo) ... end  
> &nbsp;&nbsp;mfoo(foo)
> 
> 2) make foo an instance-variable  
> &nbsp;&nbsp;@foo = [..]  
> &nbsp;&nbsp;def mfoo  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@foo.each...  
> &nbsp;&nbsp;end

You can also define the method as a closure:

&nbsp;&nbsp;&nbsp;class Module  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public :define\_method  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;moomin = 123 # local variable  
&nbsp;&nbsp;&nbsp;def foo ; moomin end # no closure  
&nbsp;&nbsp;&nbsp;(class \<\< self ; self end).  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method(:bar) { moomin } # closure

&nbsp;&nbsp;&nbsp;foo # NameError: undefined local variable or method `moomin'  
&nbsp;&nbsp;&nbsp;bar #=\> 123

But this is not a very practical or useful thing to do.  
Usually, you would be working in some class of your own,  
in which case you would simply use an instance variable.

> **···**
>
> --  
> Daniel Brockman \<daniel@brockman.se\>
> 
> &nbsp;&nbsp;&nbsp;&nbsp;So really, we all have to ask ourselves:  
> &nbsp;&nbsp;&nbsp;&nbsp;Am I waiting for RMS to do this? --TTN.
