(Newbie) Class constants from included modules

D

Dale

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
 
D

dblack

Hi --

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).
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?

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

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top