(Newbie) Class constants from included modules

I’d like to make a class constant using a module – I cannot use <
because I am extending unit_test. – Here’s the idea:

module One
@@constant=“constant”
end

module Two
def work
puts(@@constant)
end
end
class Constant_work
include One
include Two
end
Constant_work.new.work

IRB gives me:
NameError: uninitialized class variable @@constant in Two

How can I get what I want to happen?

Dale

Hi –

On Sat, 9 Jun 2007, Dale wrote:

I’d like to make a class constant using a module – I cannot use <
because I am extending unit_test. – Here’s the idea:

module One
@@constant=“constant”
end

That’s not a constant; it’s a class variable (completely different
thing).

IRB gives me:
NameError: uninitialized class variable @@constant in Two

How can I get what I want to happen?

module One
C = “constant”
end

module Two
include One
def work
puts C
end
end

class ConstantWork
include Two
end

ConstantWork.new.work # constant

Constant identifiers begin with an uppercase letter. The @@ thing is
a class variable, which is a very different (and much less useful)
construct.

David

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs