[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Backport from ruby 1.9, including constants

Victor 'Zverok' Shepelev

2/12/2007 1:29:00 AM

From: Gary Wright [mailto:gwtmp01@mac.com]
Sent: Monday, February 12, 2007 3:26 AM
>On Feb 11, 2007, at 7:41 PM, Victor Zverok Shepelev wrote:
>> Just due to this ugliness, and, additionally, I can't (or don't
>> want) modify
>> entire "A" class. More specifically, I have something like "scripts" -
>> files, which are loaded and evaluated inside some objects. For
>> example (it's
>> about new GUI library, BTW):
>
>This uses eval on a string, which can be dangerous, but if you are
>already
>reading script files and executing them....
>
>module X
> TEST = 5
>end
>
>class A; end
>a = A.new
>
>class <<a
> def instance_binding
> binding
> end
>end
>
>script = "extend X; p TEST" # could read this from a file
>
>eval script, a.instance_binding # XXX dangerous
>

Thanks, Gary.
I suppose, it's the only way to have constants from some module visible
inside some_object.instance_eval ?

V.


1 Answer

Gary Wright

2/12/2007 2:09:00 AM

0


On Feb 11, 2007, at 8:28 PM, Victor "Zverok" Shepelev wrote:
> Thanks, Gary.
> I suppose, it's the only way to have constants from some module
> visible
> inside some_object.instance_eval ?

Maybe someone else will come up with something but I think the main
problem is that instance_eval with a code block does not actually
provide the same binding context as a method definition within a class
block. That is what led me to grabbing the exact context you were
looking for with Kernel#binding and using the string version of eval.

I haven't reviewed the Ruby 1.9 constant lookup changes recently but
I think that they lean towards a more 'dynamic' rather than
'lexical' approach, which seems to be what a lot of people expect and
get surprised by in Ruby 1.8.X.

Gary Wright