From: John Wilger Date: 2006-10-28T03:44:18+09:00 Subject: Re: can there be a "with" construction? On 10/27/06, matt neuburg wrote: > Some languages have a "with" construction, where undefined methods are > routed to a designated object. Here's an example from UserTalk: > > with system.startup { > string(license) > } > > UserTalk knows what "string" is, but when it can't find "license" it > reinterprets it as system.startup.license, which works. In UserTalk, you > can even chain these tests: > > with system.temp, system.startup { > string(license) > } > Off the top of my head, you could do: $with_receivers = [] def with( *receivers ) prev_receivers = $with_receivers $with_receivers = receivers begin yield if block_given? ensure $with_receivers = prev_receivers end end def method_missing( meth, *args ) receiver = $with_receivers.detect do |r| r.respond_to? meth end if receiver return receiver.send( meth, *args ) else super end end There's definitely some nastiness there with the global variable, and I'm sure someone is going to point out where this completely falls apart within minutes of me posting it -- but it works for simple cases, at least. -- Regards, John Wilger http://johnwilger.com ----------- Alice came to a fork in the road. "Which road do I take?" she asked. "Where do you want to go?" responded the Cheshire cat. "I don't know," Alice answered. "Then," said the cat, "it doesn't matter." - Lewis Carrol, Alice in Wonderland