Created
July 17, 2010 15:22
-
-
Save gsinclair/479572 to your computer and use it in GitHub Desktop.
Ruby 1.9.2-rc1: instance_eval can't handle a lambda (needs a proc)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class InstanceEval | |
def initialize(code) | |
@code = code | |
@context = Object.new | |
end | |
def run | |
code = @code | |
result = @context.instance_eval(&code) | |
"* run --> #{result.inspect}" | |
end | |
end | |
p = proc { :p } | |
l = lambda { :l } | |
puts InstanceEval.new(p).run | |
puts InstanceEval.new(l).run | |
# The output in 1.8.7-p72 and in 1.9.1-p378: | |
# | |
# * run --> :p | |
# * run --> :l | |
# | |
# The output in 1.9.2-rc1: | |
# | |
# * run --> :p | |
# code.rb:15:... wrong number of arguments (1 for 0) (ArgumentError) | |
# from code.rb:10:in `instance_eval' |
That makes more sense. So Ruby 1.9.1 had a bug where instance_eval()
did not yield self
, and that bug was corrected in Ruby 1.9.2. According to my 1st comment, Ruby 1.8.7 also yields self
just like Ruby 1.9.2, so yes it was just an unnoticed (because block parameters are not enforced in procs & blocks) feature of instance_eval()
.
Why does instance_eval pass self
as the first argument to its argument (given that self
is already implicitly available)?
@marick, the reason is not known. Ruby 1.8.7 just does it (see example output above).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Update from nobu on ruby-core:
I was unaware of instance_eval ever yielding self, so I missed some of your meaning above, sunaku.
Now I'm wondering: when did instance_eval start yielding self, and why? Has it been going on for ages without my knowledge? ;)