Constant Lookup Confusion

J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

Trying to dynamically set a constant, but I seem to either be defining it in
the wrong place, or looking for it in the wrong place.



module ClassMaker
def self.make_class( classname , default )
theclass = Class.new do
extend ClassMethods
const_set :DEFAULT , default
end
Object.const_set classname , theclass
end

module ClassMethods
def reset
@value = DEFAULT
end
end
end

ClassMaker.make_class 'MyClass' , :my_default

begin
MyClass.reset
rescue => e
e # => #<NameError: uninitialized constant
ClassMaker::ClassMethods::DEFAULT>
end
 
A

Ammar Ali

Trying to dynamically set a constant, but I seem to either be defining it= in
the wrong place, or looking for it in the wrong place.



module ClassMaker
=C2=A0def self.make_class( classname , default )
=C2=A0 =C2=A0theclass =3D Class.new do
=C2=A0 =C2=A0 =C2=A0extend ClassMethods
=C2=A0 =C2=A0 =C2=A0const_set :DEFAULT , default
=C2=A0 =C2=A0end
=C2=A0 =C2=A0Object.const_set classname , theclass
=C2=A0end

=C2=A0module ClassMethods
=C2=A0 =C2=A0def reset
=C2=A0 =C2=A0 =C2=A0@value =3D DEFAULT
=C2=A0 =C2=A0end
=C2=A0end
end

ClassMaker.make_class 'MyClass' , :my_default

begin
=C2=A0MyClass.reset
rescue =3D> e
=C2=A0e # =3D> #<NameError: uninitialized constant
ClassMaker::ClassMethods::DEFAULT>
end

Try self::DEFAULT in #reset, that seems to do the trick.

Regards,
Ammar
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

Try self::DEFAULT in #reset, that seems to do the trick.

Regards,
Ammar
Thanks, Ammar.
 

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

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top