I hope this example will help you understand what's going on:
> Class.new { |x| puts x; def a; x; end }.new.a
#<Class:0x00007f96d877eea0>
NameError: undefined local variable or method `x' for
#<#<Class:0x00007f96d877eea0>:0x00007f96d877ed88>
This, however, will work as expected:
Class.new { |x| puts x; define_method(:a) do; x; end }.new.a
The reason for this difference is:
> Proc objects are *closures*, meaning they remember and can use the entire
context in which they were created.
This does not relate to methods defined with `def`, they do not remember
the context.
--
You received this message because you are subscribed to the Google Groups
"rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/rspec/a1bdcc35-678b-4dfd-9c0d-91d9b6bf0a0eo%40googlegroups.com.