Ceol
12/17/2006 7:36:00 AM
> So anyway, here's a starting point. The trick is to use instance_eval
> to change the value of 'self' within the block.
>
More straightforward to grab the block, make it a method of the current
object, then call that method?
require 'pp'
class ReadableThing
def book &block
ugly_call_to_get_the_eigenclass = class << self; self; end
ugly_call_to_get_the_eigenclass.send :define_method, :___secret_method,
block
send :___secret_method
end
def title
pp 'a title'
end
end
r = ReadableThing.new
r.book do
puts "self is: #{self.inspect}\n"
title
end
C:\rx\rv\bin>ruby r.rb
self is: #<ReadableThing:0x28e40fc>
"a title"
It does leave a reference to the block lying around - you'd probably want to
clean that up for production code.
-- James Moore