[newbie] make local var visible

P

Peña, Botp

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: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
 
B

Brian Schröder

Hi All,
=20
I may not be feeling well today, so pardon me pls.
=20
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).
=20
I encountered a wall though.
=20
Consider,
=20
cat test.rb
=20
#sample script---------------->8
foo=3D["a","b","c"]
=20
def mfoo
foo.each do |f|
p f
end
end
=20
# go-go-go!
mfoo
#end sample script------------>8
=20
i just enclosed the some commands with def mfoo / end to make a new metho= d
mfoo and make my script cleaner, and besides, i may be calling mfoo again=
 
C

Cristi BALAN

Hi All,
=20
I may not be feeling well today, so pardon me pls.
=20
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).
=20
I encountered a wall though.
=20
Consider,
=20
cat test.rb
=20
#sample script---------------->8
foo=3D["a","b","c"]
=20
def mfoo
foo.each do |f|
p f
end
end
=20
# go-go-go!
mfoo
#end sample script------------>8
=20
i just enclosed the some commands with def mfoo / end to make a new metho= d
mfoo and make my script cleaner, and besides, i may be calling mfoo again=
 
W

wannes

#sample script---------------->8
foo=3D["a","b","c"]
=20
def mfoo
foo.each do |f|
p f
end
end
=20
# go-go-go!
mfoo
#end sample script------------>8
test.rb:4:in `mfoo': undefined local variable or method `foo'
from test.rb:9
So how can i force foo visible to mfoo()?

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.
def mfoo(foo) ... end
mfoo(foo)
2) make foo an instance-variable
@foo =3D [..]
def mfoo
@foo.each...
end

grtz,
wannes
 
D

Daniel Brockman

wannes said:
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.
def mfoo(foo) ... end
mfoo(foo)

2) make foo an instance-variable
@foo = [..]
def mfoo
@foo.each...
end

You can also define the method as a closure:

class Module
public :define_method
end

moomin = 123 # local variable
def foo ; moomin end # no closure
(class << self ; self end).
define_method:)bar) { moomin } # closure

foo # NameError: undefined local variable or method `moomin'
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.
 
T

Trans

You could do:

Foo=["a","b","c"]

def mfoo
Foo.each do |f|
p f
end
end

# go-go-go!
mfoo

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

class Bar

foo = ["a","b","c"] # attribute

def mfoo
foo.each do |f|
p f
end
end

end

But the only way to do that woud be:

1) Irregular attr syntax.

attr foo = ["a","b","c"]

2) Alternate assignment operator.

foo := ["a","b","c"]

3) Prefixed local vars.

%local_var = 'what have you'

I myself have suggested before that

foo = 10

be essentially equivalent to

def foo ; 10 ; end

And that constants and methods share the same namespace.

T.
 
D

David Vallner

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)
# do stuff with a copy of arg.
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,949
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top