From: "David A. Black" Date: 2007-08-24T10:06:40+09:00 Subject: Re: Changing "self" for cleaner code Hi -- On Fri, 24 Aug 2007, Morton Goldberg wrote: > On Aug 23, 2007, at 5:45 AM, Gregor Kopp wrote: > >> Another way: >> >> class Object >> def with &block >> yield self >> end >> end >> >> verylongobject = "Hakuna Matata!" >> >> verylongobject.with do |o| >> puts o #-> Kakuna Matata! >> puts o.length #-> 14 >> puts o.reverse #-> !atataM anukaH >> puts o.upcase #-> HAKUNA MATATA! >> end > > With a small modification the OP can get exactly the syntactic form he wants: > > > def with obj > yield obj > end > > verylongobject = "Hakuna Matata!" > > with verylongobject do |o| > o # => "Hakuna Matata!" > o.length # => 14 > o.reverse # => "!atataM anukaH" > o.upcase # => "HAKUNA MATATA!" > end > Is that better than just calling the methods without a block? Or if the variable length is a concern: o = verylongobject David -- * Books: RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242) RUBY FOR RAILS (http://www.manning.com/black) * Ruby/Rails training & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)