On Apr 12, 2005, at 11:21 PM, Curt Sampson wrote:

>
> Presumably there's some theory behind this, but this is a seeming
> inconsistency in the language that I don't understand.
>
>     foo = "foo"
>     C = Class.new() {
> 	def method1; puts foo; end

Here method1's local variable (as there's no foo() method so far) is 
being referenced and it is not defined.

> 	define_method(:method2) { puts foo }

Here a block is used that defines the closure referencing the local 
variable foo from the current scope.
Looks pretty consistent ;-)

>     }

Oh, yes, a block is used to define a class body, I see. However, "def" 
syntax prevents "foo" from being treated as a closure participant. At 
least, it is how it looks to me ;-).

>
>     C.new.method2	# prints "foo"
>     C.new.method1	# NameError: undefined local variable or method `foo'
>
> Can someone explain why this is sensible behaviour?
>
> cjs
> -- 
> Curt Sampson  <cjs / cynic.net>   +81 90 7737 2974   
> http://www.NetBSD.org
>      Make up enjoying your city life...produced by BIC CAMERA
>
>

Gennady.