Hi --

On Thu, 4 Sep 2008, Patrick Li wrote:

> So I thought about your code snippet a bit more Robert, and it's almost
> what I need. Which is, the ability to access the local scope, but also
> have access to the methods that are granted by the html-do-end method.
>
> so in your example, you can now do this:
>
> html do
>  image(@source)
> end
>
> but you still can't do something this:
>
> html do
>  @code = retrieveCode
> end
> puts @code <-- doesn't work, because this is a different @code than the
> one you assigned to.
>
> I got around this by writing all the instance variables in "context" out
> again at the end of the block. But it's not a very clean solution.

I personally dislike the "stealth" instance_eval, since it does indeed
look like instance variables belong to one object when they actually
belong to another. One thing you can do:

   class C
     def some_method
       yield self
     end
   end

   @a = 1
   C.new.some_method do |c|
     c.another_method(@a)     # etc.
   end

so that you have the object you need but without changing 'self'
during the block.


David

-- 
Rails training from David A. Black and Ruby Power and Light:
   Intro to Ruby on Rails  January 12-15   Fort Lauderdale, FL
   Advancing with Rails    January 19-22   Fort Lauderdale, FL *
   * Co-taught with Patrick Ewing!
See http://www.rubypal.com for details and updates!