From: Csaba Henk Date: 2005-04-02T22:54:49+09:00 Subject: Re: changing the context in which a block executes On 2005-03-30, John-Mason P. Shackelford wrote: > Greetings. > > I'd like to define a method which accepts a block such callers can write: > > some_method do > op1(arg) > op2(arg) > end > > instead of: > > some_method do |obj| > obj.op1(arg) > obj.op2(arg) > end [snip] > My question is, is this the best way to achieve this in Ruby? The block passed to some_method should be called by instance_eval. class Foo def printf(x) 45 end def some_method &b instance_eval &b end end Foo.new.some_method { printf "a" } # => 45 Csaba