Pit Capitain
4/13/2005 6:55:00 AM
Curt Sampson schrieb:
>
> 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
> define_method(:method2) { puts foo }
> }
>
> C.new.method2 # prints "foo"
> C.new.method1 # NameError: undefined local variable or method `foo'
>
> Can someone explain why this is sensible behaviour?
Your code can be reduced to
foo = "foo"
def m1; p foo; end
m2 = lambda { p foo }
m2.call # => "foo"
m1.call # => NameError
Does this help?
Regards,
Pit