Constant Lookup Confusion

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

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

Regards,
Ammar

···

On Tue, Oct 19, 2010 at 9:52 AM, Josh Cheek <josh.cheek@gmail.com> wrote:

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

Thanks, Ammar.

···

On Tue, Oct 19, 2010 at 2:51 AM, Ammar Ali <ammarabuali@gmail.com> wrote:

On Tue, Oct 19, 2010 at 9:52 AM, Josh Cheek <josh.cheek@gmail.com> wrote:
> 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
>

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

Regards,
Ammar