I’m a bit confused by the scope of local variables.
$ irb --simple-pompt
foo = “bar”
=> “bar”
def function
puts foo
end
=> nil
function
NameError: undefined local variable or method foo' for #<Object:0xf9000> from (irb):3:in function’
from (irb):5
Since ‘foo’ was defined before ‘function’ and both ‘function’ and ‘foo’
are in the same scope (I think) shouldn’t ‘function’ see ‘foo’?
···
–
Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137
At Tue, 21 Jan 2003 04:32:59 +0900, Daniel Carrera wrote:
$ irb --simple-pompt
foo = “bar”
=> “bar”
def function
puts foo
end
=> nil
function
NameError: undefined local variable or method foo' for #<Object:0xf9000> from (irb):3:in function’
from (irb):5
Since ‘foo’ was defined before ‘function’ and both ‘function’ and ‘foo’
are in the same scope (I think) shouldn’t ‘function’ see ‘foo’?
I’m a bit confused by the scope of local variables.
$ irb --simple-pompt
foo = “bar”
=> “bar”
def function
puts foo
end
=> nil
function
NameError: undefined local variable or method foo' for #<Object:0xf9000> from (irb):3:in function’
from (irb):5
Since ‘foo’ was defined before ‘function’ and both ‘function’ and ‘foo’
are in the same scope (I think) shouldn’t ‘function’ see ‘foo’?
That’s kind of how Perl’s scoping works, but not Ruby. The scope inside
your def function is different from the scope outside. So inside of
method ‘function’ there is no ‘knowledge’ of the foo variable that was
defined outside.
Phil
“Or perhaps the truth is less interesting than the facts?”
Amy Weiss (accusing theregister.co.uk of engaging in ‘tabloid journalism’)
Senior VP, Communications
Recording Industry Association of America
Since ‘foo’ was defined before ‘function’ and both ‘function’ and ‘foo’
are in the same scope (I think) shouldn’t ‘function’ see ‘foo’?
You can do this in 1.7:
module M
foo = “bar”
define_method :function do
puts foo
end
end
include M
function # prints: bar
This falls into the catagory of
one-of-those-1.8-features-I-didn’t-know-about.
So define_method can define a method that ‘knows’ about all of the
variables in the scope in which it is defined? Useful, but one needs to
be careful.
Phil
–
“Or perhaps the truth is less interesting than the facts?”
Amy Weiss (accusing theregister.co.uk of engaging in ‘tabloid journalism’)
Senior VP, Communications
Recording Industry Association of America
irb(main):001:0> @@foo = “bar”
“bar”
irb(main):002:0> def function
irb(main):003:1> puts @@foo
irb(main):004:1> end
nil
irb(main):005:0> function
bar
nil
irb(main):006:0>
Note the “@@” which makes “foo” a module variable in the current module,
which thus can be seen by method “function”.
I’m a bit confused by the scope of local variables.
$ irb --simple-pompt
foo = “bar”
=> “bar”
def function
puts foo
end
=> nil
function
NameError: undefined local variable or method foo' for #<Object:0xf9000> from (irb):3:in function’
from (irb):5
Since ‘foo’ was defined before ‘function’ and both ‘function’ and ‘foo’
are in the same scope (I think) shouldn’t ‘function’ see ‘foo’?
That’s kind of how Perl’s scoping works, but not Ruby. The scope inside
your def function is different from the scope outside. So inside of
method ‘function’ there is no ‘knowledge’ of the foo variable that was
defined outside.
Phil
“Or perhaps the truth is less interesting than the facts?”
Amy Weiss (accusing theregister.co.uk of engaging in ‘tabloid
journalism’)
···
Daniel Carrera dcarrera@math.umd.edu wrote:
Senior VP, Communications
Recording Industry Association of America
module M
foo = “bar”
define_method :function do
puts foo
end
end
include M
function # prints: bar
This falls into the catagory of
one-of-those-1.8-features-I-didn’t-know-about.
So define_method can define a method that ‘knows’ about all of the
variables in the scope in which it is defined? Useful, but one needs to
be careful.
Correct me if I’m wrong, but isn’t this simply
because the do/end block itself is able to “see”
the variable foo?
The body of an ordinary method, i.e., def/end,
looks similar to a do/end block but isn’t one.
This starts my mind speculating about the two
converging into one thing… but that would
likely raise more issues than it resolves. And
I certainly am not in the mood to think it
through.
Hal
···
----- Original Message -----
From: “Phil Tomson” ptkwt@shell1.aracnet.com
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Monday, January 20, 2003 3:54 PM
Subject: Re: Local variable scope
module M
foo = “bar”
define_method :function do
puts foo
end
end
include M
function # prints: bar
[snip]
Correct me if I’m wrong, but isn’t this simply
because the do/end block itself is able to “see”
the variable foo?
The body of an ordinary method, i.e., def/end,
looks similar to a do/end block but isn’t one.
The following question is probably completely different to this thread,
but I’m going to ask it anyway (with the caveat for readers that I’ve
been playing with Ruby for, err, a week now).
I tried to do this yesterday:
def foo(foobar)
bar = 0;
if foobar != nil
bar = foobar * 4
else
bar = “foobar!”
end
bar
end
foo(3) → 0
from looking at it it seems that my problem is that the bar inside the
if/else block is not the same bar that is declared at the start of the
method.
how should I be doing the above? I ended up doing this:
def foo(foobar)
bar =
if foobar != nil
bar = foobar * 4
else
bar = “foobar!”
end
bar
end
which worked fine, but I was wondering if there was a way to do what I
was trying initially.
cheers and thanks
dim
(ps - code written from memory, and obviously a silly example, not the
real thing)
def foo(foobar)
bar = 0;
if foobar != nil
bar = foobar * 4
else
bar = “foobar!”
end
bar
end
This works fine in my system. Check your code. Try this example on your
system.
always the way… ok, by works fine - I’m assuming the intent is
obvious (in that I expect it to return the arg multiplied by 4, or the
string “foobar!” if there is no arg passed in.
I’ve changed my old code - and didn’t checkin a broken version so dont
have what I was doing… but from what you’re saying this ought to work
as I expect it to and it was no doubt just me doing something stupid.
cheers
dim
···
On Tue, Jan 21, 2003 at 08:35:02AM +0900, Dmitri Colebatch wrote:
Yes… actually I guess you’d have to default it to
nil if you actually wanted it to accept NO ARGUMENT
passed in: def foo(foobar=nil)
Hal
···
----- Original Message -----
From: “Dmitri Colebatch” dim@colebatch.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Monday, January 20, 2003 5:47 PM
Subject: Re: Local variable scope
always the way… ok, by works fine - I’m assuming the intent is
obvious (in that I expect it to return the arg multiplied by 4, or the
string “foobar!” if there is no arg passed in.
Just to emphasise the point. If you call your code with no
argument, the parser or whever you call it will spit the dummy. Just
as zero is a number, nil qualifies as an argument.
Gavin
···
On Tuesday, January 21, 2003, 10:52:40 AM, Hal wrote:
----- Original Message -----
From: “Dmitri Colebatch” dim@colebatch.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Monday, January 20, 2003 5:47 PM
Subject: Re: Local variable scope
always the way… ok, by works fine - I’m assuming the intent is
obvious (in that I expect it to return the arg multiplied by 4, or the
string “foobar!” if there is no arg passed in.
Yes… actually I guess you’d have to default it to
nil if you actually wanted it to accept NO ARGUMENT
passed in: def foo(foobar=nil)
Yes… actually I guess you’d have to default it to
nil if you actually wanted it to accept NO ARGUMENT
passed in: def foo(foobar=nil)
Just to emphasise the point. If you call your code with no
argument, the parser or whever you call it will spit the dummy. Just
as zero is a number, nil qualifies as an argument.
I’d effectively get a ‘array index out of bounds’ error or something
yeah? in that the array of arguments is zero length and because I
require one argument I’m doing something like: