Morton Goldberg
10/25/2006 8:09:00 PM
On Oct 25, 2006, at 3:54 PM, dblack@wobblini.net wrote:
> 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.
I would expect that behavior and, in the situation I'm dealing with,
I think it's an acceptable trade-off.
Regards, Morton