Project

General

Profile

Bug #5288 » method_to_proc.rb

ernie (Ernie Miller), 09/07/2011 08:51 AM

 
#!/usr/bin/env ruby
# http://www.ruby-doc.org/core-1.8.7/classes/Object.html#M000006 :
#
# 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
def self.foo
puts self
end

def bar
puts self
end
end

class Executioner
def exec(&block)
instance_exec &block
end
end

exe = Executioner.new # => #<Executioner:0x007f8cb884d108>

exe.exec { puts self } # => #<Executioner:0x007f8cb884d108>

exe.exec &lambda { puts self } # => #<Executioner:0x007f8cb884d108>

exe.exec &Classy.method(:foo).clone # => Classy

exe.exec &Classy.new.method(:bar).clone # => #<Classy:0x007f8cb884ce10>
    (1-1/1)