Hi -- On Thu, 26 Oct 2006, Morton Goldberg wrote: > On Oct 25, 2006, at 3:31 PM, Gavin Kistner wrote: > >> From: Morton Goldberg [mailto:m_goldberg / ameritech.net] >>> I'd rather pass in a block. Is there a way to do that? Something like: >>> >>> <pseud-code> >>> module Kernel >>> def tell(obj, &to_do) >>> # what goes here? >>> end >>> end >>> >>> tell Foo.new do >>> report >>> eat 'burger', 'fries' >>> drink 'beer' >>> be_merry >>> end >>> </pseud-code> >> >> def tell( obj, &to_do ) >> obj.instance_eval( &to_do ) >> end >> >> class Foo >> def say_hi >> p "Hi!" >> end >> end >> >> tell Foo.new do >> say_hi >> end >> #=> "Hi!" > > It's really that easy? Amazing! Ruby makes things so easy compared with the > other languages I've used that I'm constantly inventing hard ways of doing > what is too obvious for me to see. It's easy, but it can also be a bit obfuscating. For example: class C def initialize(thing) @thing = thing end def tell(&block) instance_eval(&block) end end c = C.new("Hi") @thing = "Hello" c.tell do puts @thing # Hi end So you get some perhaps unwanted variable-shadowing, and similarly with method calls. 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