From: Bob Hutchison Date: 2006-07-24T01:19:39+09:00 Subject: Re: [ANN] Mongoose 0.2.0 Hi, On Jul 21, 2006, at 11:10 PM, John W. Long wrote: > Jamey Cribbs wrote: >> The way it works is that in the Plane.find method, I grab the >> block and pass it to #instance_eval. This executes the block in >> the Plane classes environment, so it knows that when it sees the >> attribute "speed" or "country" in the block, it knows to call >> Plane.speed and Plane.country. Planes.speed happens to be a >> reference to a SkiplistColumn instance and it knows how to handle >> the "> 350" passed to it. > > The problem with this approach is that you loose access to instance > variables. For instance: > > >> class Object > >> def with(&block) > >> instance_eval(&block) > >> end > >> end > => nil > >> @test = 1 > => 1 > >> Object.new.with { puts @test } > => nil > > In the above example @test when used within the context of the with > block is thought to be an instance variable of the object, which is > why it returns nil instead of 1. I consider this behavior undesirable. Is this really a problem? If we write it this way: class Object def with(&block) instance_eval(&block) end end class Toy def play @test = 'hello there' with { puts "!!!! [#{@test}] !!!!" } end end toy = Toy.new toy.play you get the output: !!!! [hello there] !!!! which is using the @test as I think you want. Cheers, Bob > > -- > John Long > http://wiseheartdesign.com > ---- Bob Hutchison -- blogs at Recursive Design Inc. -- Raconteur -- xampl for Ruby --