Forum: Ruby Lexical vs Dynamic Scope

C9badb8b30cf30d420552e026a921499?d=identicon&s=25 Tim Morgan (Guest)
on 2011-01-22 07:20
(Received via mailing list)
Forgive this very basic question, but Googling has not answered my
question, and I'm sure it's a simple one for the gurus here.

I've been using Ruby for years, and I've always had questions about
how it handles scope. Usually Ruby just does what I would expect it
to.

In reading about lexical vs. dynamic scope on various places on the
Web, I read that Ruby has lexical (static) scope. But I cannot prove
it to myself with code. For example, the following produces one (1) --
not zero (0) as I would expect it to if Ruby was truly statically
scoped:

x = 0
f = Proc.new { x }
g = Proc.new {
  x = 1
  f.call
}
puts g.call
# => 1

(I purposely used Procs instead of regular methods here since Ruby
methods cannot see the top-level "x" variable at all, which is a whole
other issue.)

Is Ruby really dynamically scoped?
B31e7abd14f1ceb4c4957da08933c630?d=identicon&s=25 Josh Cheek (josh-cheek)
on 2011-01-22 08:16
(Received via mailing list)
On Sat, Jan 22, 2011 at 12:20 AM, Tim Morgan <tim@timmorgan.org> wrote:

> not zero (0) as I would expect it to if Ruby was truly statically
>
> (I purposely used Procs instead of regular methods here since Ruby
> methods cannot see the top-level "x" variable at all, which is a whole
> other issue.)
>
> Is Ruby really dynamically scoped?
>
>
The x in g is the same as the x outside of g. There is only one single x
in
your program, so this doesn't show much. Maybe this instead?

def show_error(f)
  f.call
rescue => e
  e
end

f = lambda { x }
show_error f # => #<NameError: undefined local variable or method `x'
for
main:Object>

x = 1
show_error f # => #<NameError: undefined local variable or method `x'
for
main:Object>

g = lambda { x }
show_error g # => 1
C9badb8b30cf30d420552e026a921499?d=identicon&s=25 Tim Morgan (Guest)
on 2011-01-22 17:49
(Received via mailing list)
On Jan 22, 1:12am, Josh Cheek <josh.ch...@gmail.com> wrote:
> The x in g is the same as the x outside of g. There is only one single x in
> your program, so this doesn't show much. Maybe this instead?

Ahhh. I see. Thanks!
09a32175057418748822c587ac08c429?d=identicon&s=25 Abinoam Jr. (abinoampraxedes_m)
on 2011-01-22 20:20
(Received via mailing list)
On Sat, Jan 22, 2011 at 4:12 AM, Josh Cheek <josh.cheek@gmail.com>
wrote:
> x = 1
> show_error f # => #<NameError: undefined local variable or method `x' for
> main:Object>
>
> g = lambda { x }
> show_error g # => 1


After doing the above, try this.

def x
  "Anything returned by the (Receiver).x method"
end

show_error f # => "Anything returned by the (Receiver).x method"
show_error g # => 1

At compile time...
f = lambda { x }
x is not defined, and is thought to be a method!

Look...
http://www.ruby-forum.com/topic/724184

Abinoam Jr.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.