From: "Mauricio Fernández" Date: 2003-05-17T04:01:06+09:00 Subject: Re: procs and context On Sat, May 17, 2003 at 02:43:57AM +0900, repeatr wrote: > def method_missing(action) > puts "#{action}...in the city" > end > > class Room > def method_missing(action) > puts "#{action}...in the the privacy of your own room" > end > > def live(&block) > instance_eval { Proc.new.call } Proc.new reifies the passed block, which was already bound to the external context. Use instance_eval(&block). Note however that only the value of self is substituted, ie.: batsman@tux-chan:/tmp$ expand -t2 ag.rb class Foo def bar(&block) a = "inner a" instance_eval(&block) end end a = "external a" o = Foo.new o.bar do puts "self.id: #{self.id}" puts "a is #{a.inspect}" end puts "o.id: #{o.id}" batsman@tux-chan:/tmp$ ruby ag.rb self.id: 537725500 a is "external a" o.id: 537725500 > end > > def initialize > put_feet_on_table > snore > end > end > > walk > go_to_work > > room = Room.new > room.live do > walk_around_naked! > end > -------------- > > output: > walk...in the city > go_to_work...in the city > put_feet_on_table...in the the privacy of your own room > snore...in the the privacy of your own room > walk_around_naked!...in the city LOL We *have to* put this in some tutorial or documentation! -- _ _ | |__ __ _| |_ ___ _ __ ___ __ _ _ __ | '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \ | |_) | (_| | |_\__ \ | | | | | (_| | | | | |_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_| Running Debian GNU/Linux Sid (unstable) batsman dot geo at yahoo dot com - DDD no longer requires the librx library. Consequently, librx errors can no more cause DDD to crash. -- DDD