> -----Original Message-----
> From: Mauricio Fernandez [mailto:batsman.geo / yahoo.com]
> Sent: 16 May 2003 09:01
> To: ruby-talk ML
> 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

Zoi that is enlightening, with this you've cleansed my mind of all the moth
flutterings and dust wisps.

If I may talk myself into understanding, thus with:
instance_eval(&Proc.new)
the proc is converted into a block with its sense of "self" converted.

while with:
instance_eval { Proc.new.call }
a block is created with a self pointing to this object (in this case
redundantly so), while the original proc yonder the mountains is executed
untouched.


>
> >   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!

I hope it doesn't cause any bad dreams though ;-)

a tip of the hat
Peter