[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Backport from ruby 1.9, including constants

Victor 'Zverok' Shepelev

2/12/2007 12:03:00 AM

Hello all.

During last several monthes I've worked on some library, using the latest
ruby1.9. Now I want to release the library to community, but first I need to
"backport" it.
One small problem I've stumbled upon:

module Constants
TEST = 5
end

class A
end

a = A.new

a.instance_eval{
extend Constants
p TEST #<== here
}

At the "here" string, ruby1.9 had printed "5", but ruby 1.8.5 raises
NameError (uninitialized constant TEST).

I know, the question is silly, but I can't find how to do this. (by some
reasons, the code should affect only one object, not entire class)

Thanks.

V.


3 Answers

Gary Wright

2/12/2007 12:33:00 AM

0


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
> }

The only solutions I can think of involve explicitly referencing the
class:

class Object
def singleton_class
(class <<self; self; end)
end
end

a.instance_eval {
extend Constants
singleton_class::TEST
}

But at that point it is probably easier to simply reference Constants
directly:

a.instance_eval {
t = Constants::TEST
}

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...
}


Gary Wright




Ken Bloom

2/12/2007 1:08:00 AM

0

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

> Hello all.
>
> During last several monthes I've worked on some library, using the latest
> ruby1.9. Now I want to release the library to community, but first I need to
> "backport" it.
> One small problem I've stumbled upon:
>
> module Constants
> TEST = 5
> end
>
> class A
> end
>
> a = A.new
>
> a.instance_eval{
> extend Constants
> p TEST #<== here
> }
>
> At the "here" string, ruby1.9 had printed "5", but ruby 1.8.5 raises
> NameError (uninitialized constant TEST).
>
> I know, the question is silly, but I can't find how to do this. (by some
> reasons, the code should affect only one object, not entire class)

class << a
extend Constants
p TEST
end


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

Joel VanderWerf

2/12/2007 3:59:00 AM

0

Victor "Zverok" Shepelev wrote:
> Hello all.
>
> During last several monthes I've worked on some library, using the latest
> ruby1.9. Now I want to release the library to community, but first I need to
> "backport" it.
> One small problem I've stumbled upon:
>
> module Constants
> TEST = 5
> end
>
> class A
> end
>
> a = A.new
>
> a.instance_eval{
> extend Constants
> p TEST #<== here
> }
>
> At the "here" string, ruby1.9 had printed "5", but ruby 1.8.5 raises
> NameError (uninitialized constant TEST).

module Constants
TEST = 5
end

class A
end

a = A.new

m = Module.new
def m.const_missing k
Constants.const_get(k) || super(k)
end

string_from_file = <<END
TEST
END

test = m.module_eval %{
a.instance_eval {
#{string_from_file}
}
}

p test # ==> 5

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407