[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 12:41:00 AM

From: Gary Wright [mailto:gwtmp01@mac.com]
Sent: Monday, February 12, 2007 2:33 AM
>On Feb 11, 2007, at 7:02 PM, Victor Zverok Shepelev wrote:
>
>> module Constants
>> TEST = 5
>> end
>>
>> class A
>> end
>>
>> a = A.new
>>
>> a.instance_eval{
>> extend Constants
>> p TEST #<== here
>> }
>
[...]
>
>It does seem a bit strange to be adding constants to the singleton
>class.
>Why not add them to A itself? That way you don't need to extend the
>singleton class with the Constants module.
>
>class A
> include Constants
>end
>
>A.new.instance_eval {
> p self.class::TEST # still a bit ugly...
>}
>

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):

File tabs.rs ("rs" for "Ruby Script" :)
------
extend Win32::Keys
on(TAB) do
...blah
end

on(SHIFT, TAB) do
... blah, blah
end
-----

This "script" is represents "tabs" behavior and it is evaluated in context
of UI element, which I want this behavior to be added.
The more verbose version, which is enforced by ruby 1.8.5, I don't like at
all:

on(Win32::Keys::TAB) do #fuuuuuu!

It's a problem, so :(

V.


2 Answers

Gary Wright

2/12/2007 1:26:00 AM

0


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







Ken Bloom

2/12/2007 2:08:00 AM

0

On Mon, 12 Feb 2007 09:41:24 +0900, Victor \"Zverok\" Shepelev wrote:

> From: Gary Wright [mailto:gwtmp01@mac.com]
> Sent: Monday, February 12, 2007 2:33 AM
>>On Feb 11, 2007, at 7:02 PM, Victor Zverok Shepelev wrote:
>>
>>> module Constants
>>> TEST = 5
>>> end
>>>
>>> class A
>>> end
>>>
>>> a = A.new
>>>
>>> a.instance_eval{
>>> extend Constants
>>> p TEST #<== here
>>> }
>>
> [...]
>>
>>It does seem a bit strange to be adding constants to the singleton
>>class.
>>Why not add them to A itself? That way you don't need to extend the
>>singleton class with the Constants module.
>>
>>class A
>> include Constants
>>end
>>
>>A.new.instance_eval {
>> p self.class::TEST # still a bit ugly...
>>}
>>
>
> 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):
>
> File tabs.rs ("rs" for "Ruby Script" :)
> ------
> extend Win32::Keys
> on(TAB) do
> ...blah
> end
>
> on(SHIFT, TAB) do
> ... blah, blah
> end
> -----
>
> This "script" is represents "tabs" behavior and it is evaluated in context
> of UI element, which I want this behavior to be added.
> The more verbose version, which is enforced by ruby 1.8.5, I don't like at
> all:
>
> on(Win32::Keys::TAB) do #fuuuuuu!
>
> It's a problem, so :(
>
> V.

How about

C=Win32::Keys
on(C::TAB) do
...blah
end

on(C::SHIFT, C::TAB) do
... blah, blah
end

(or even use c instead of C so you don't allocate another constant)


--
Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu...