Skip to content

Instantly share code, notes, and snippets.

@ernie ernie/lies.rb
Created Sep 6, 2011

What would you like to do?
instance_exec LIES!!!
#!/usr/bin/env ruby
# :
# obj.instance_exec(arg...) {|var...| block } => obj
# Executes the given block within the context of the receiver (obj).
# In order to set the context, the variable self is set to obj while
# the code is executing, giving the code access to obj‘s instance variables.
# Arguments are passed as block parameters.
class Classy
puts self
def bar
puts self
class Executioner
def exec(&block)
instance_exec &block
exe = # => #<Executioner:0x007f8cb884d108>
exe.exec { puts self } # => #<Executioner:0x007f8cb884d108>
exe.exec &lambda { puts self } # => #<Executioner:0x007f8cb884d108>
exe.exec &Classy.method(:foo) # => Classy
exe.exec & # => #<Classy:0x007f8cb884ce10>

This comment has been minimized.

Copy link
Owner Author

ernie commented Sep 6, 2011

So, I understand there are various reasons why a method object would want/need to hang onto its own binding even when asked to do otherwise, but I have to admit I was surprised and disappointed at these results. :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.