From: dblack@... Date: 2006-10-26T12:29:48+09:00 Subject: Re: Pass block instead of here document? Hi -- On Thu, 26 Oct 2006, Daniel N wrote: > My understanding of blocks is at best still very shakey, but is there a > reason in all these that something like > > def tell > yield self > end > > would not have the desired effect. > > Then with appropriate method missings etc you could call whatever you like > in the block. If the drink and eat methods are already defined in the > object, they would be called as appropriate. > > so to use it. > > obj = Foo.new > obj.tell do > eat 'burger', 'fries' > drink 'beer' > end > > Is there a reason that this would not have the same effect as the > instance_eval methods? Yes: instance_eval temporarily changes "self", and that means that bareword method calls like eat and drink will be directed to the Foo object. Otherwise, it's just like doing: eat in the middle of a program where you haven't defined an eat method. David -- David A. Black | dblack@wobblini.net Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org