exbinary
3/5/2009 - 6:58 PM

Procs as closures

Procs as closures

class One
  def create_proc
    Proc.new { puts self.class }
  end
end

p = One.new.create_proc
p.call                  # => One

class Two
  def yield_to_proc
    yield
  end
  def call_proc &block
    block.call
  end
end

Two.new.yield_to_proc &p   # => One
Two.new.call_proc &p       # => One

Two.class_eval do
  define_method(:this_one_is_different, &p)
end

Two.new.this_one_is_different   # => Two
Two.new.instance_eval &p        # => Two