From: Morton Goldberg Date: 2006-10-26T05:03:27+09:00 Subject: Re: Pass block instead of here document? On Oct 25, 2006, at 3:39 PM, Tim Pease wrote: > On 10/25/06, Morton Goldberg wrote: >> I have a situation where I want to send many messages, one after the >> other, to single object. Instead of writing, say, >> > > > >> >> Foo.new.perform { report; eat 'burger', 'fries'; drink 'beer'; >> be_merry } >> >> > > class Foo > def initialize( &block ) > return unless block_given? > self.instance_eval &block > end > > def eat( *args ) > puts args.inspect > end > > def drink( *args ) > puts args.inspect > end > end > > > Foo.new do > eat 'burger', 'fries' > drink 'beer' > end > > Is that what you are looking for? You can just as easily create a > process method (instead of using the initialize method) to do > something like this ... > > f = Foo.new > f.process do > eat 'burger', 'fries' > drink 'beer' > end > > Blessings, > TwP It's the second way that I was looking for. Now that I know that Object#instance_eval will accept a block, I can see how easy it is to write Foo#perform Thanks for your help. Regards, Morton