Sorry but it does not work:

class A
  def doit(&block)
    instance_eval { block[self] }
  end
  def sayhello(who)
    puts "hello #{who}: #{object_id}"
  end
end

A.new.doit do |who|
  sayhello("me")
  who.sayhello("who")
end

in `block in <main>': undefined method `sayhello' for main:Object
(NoMethodError)

It seems that the block is not executed in the context of A.new anymore.

I'm not sure that what I want makes sense because I've got self inside
the block, but if anyone knows how to make this code working in Ruby
1.9, I'm still interested.

blambeau


On Thu, Jan 8, 2009 at 3:44 PM, Brian Candler <b.candler / pobox.com> wrote:
> LAMBEAU Bernard wrote:
>> obj.instance_eval {| | block } => obj
>>
>> I don't interpret it that way: it only states that self is obj inside
>> the block. The documentation says nothing about block arguments.
>>
>> I would like something like this:
>>
>> obj.instance_eval {| who| block } => obj
>> where who ==self == obj.
>
> You can build that yourself.
>
> class Object
>  def my_instance_eval(&blk)
>    instance_eval { blk[self] }
>  end
> end
>
> obj = ""
> obj.my_instance_eval { |who| p who }
> --
> Posted via http://www.ruby-forum.com/.
>
>