[Ruby] eval question

Aaron Patterson aaron at tenderlovemaking.com
Mon Apr 30 13:34:40 PDT 2007


On Mon, Apr 30, 2007 at 02:23:05PM -0700, Ali Rizvi wrote:
> Your variable (phil_collins) is limited in scope within the eval while your
> method is in the main/kernel scope which lives on after the eval while
> variable seizes to exist beyond that scope.

I figured as much, but it just seemed weird to me that one would live on and
the other wouldn't.

> 
> The following would work:
> 
>   eval(<<END
>     phil_collins = 'awesome'
>     def easy_lover
>       "She'll get a hold on you believe it"
>     end
>     puts phil_collins
>   END
>   )
>   puts easy_lover

I would, however, like to be able to access the 'phil_collins' variable
outside the eval block.  I don't really *need* to access it outside the
eval, it would just be nice if I could.

-- 
Aaron Patterson
http://tenderlovemaking.com/


More information about the Ruby mailing list