Skip to content

Instantly share code, notes, and snippets.

@gsinclair
Created July 17, 2010 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gsinclair/479572 to your computer and use it in GitHub Desktop.
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)
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'
@sunaku
Copy link

sunaku commented Jul 18, 2010

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().

@marick
Copy link

marick commented Jan 8, 2012

Why does instance_eval pass self as the first argument to its argument (given that self is already implicitly available)?

@sunaku
Copy link

sunaku commented Jan 9, 2012

@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