From: "David A. Black" Date: 2004-04-14T09:59:52+09:00 Subject: Re: Interesting Scoping Rules Hi -- On Wed, 14 Apr 2004, Charles Comstock wrote: > > val = "fem" > class Test > puts val > end > > # or > > class Test > val = "A" > def info > puts val > end > end > > Test.new.info > > # or > > val = 1 > def t > puts val > end > t > > Why is it none of these are allowed? All of these seem like perfectly > logical places that lexical scoping would allow. It really suprised me > that none of thse work, anyone have an explanation for this behavior? My explanation is kind of circular, I guess: those variables are out of scope because def starts a new scope :-) I'm so used to it I don't really notice it. I don't have any real technical insight into why it's that way. But I just wanted to add... don't forget you can use #define_method if you want to keep the same scope: class A a = 1 define_method("b", lambda { puts a }) end A.new.b # 1 David -- David A. Black dblack@wobblini.net