Module#const_missing redefinition

I try to redefine #const_missing but it does not behave as I expect.

def Object.const_missing(sym)
   puts "%s.const_missing: %s" % [self, sym]
end

class Test
   def Test.const_missing(sym)
     puts "%s.const_missing: %s" % [self, sym]
   end

   # OK: Test.const_missing
   Bart
end

Test.new.instance_eval do
   # Object.const_missing, why not Test.const_missing?
   Bart
end

Test.module_eval do
   # Object.const_missing, why not Test.const_missing?
   Bart
end

Regards.

Hi --

···

On Thu, 6 Oct 2005, El Barto wrote:

I try to redefine #const_missing but it does not behave as I expect.

def Object.const_missing(sym)
puts "%s.const_missing: %s" % [self, sym]
end

class Test
def Test.const_missing(sym)
   puts "%s.const_missing: %s" % [self, sym]
end

# OK: Test.const_missing
Bart
end

Test.new.instance_eval do
# Object.const_missing, why not Test.const_missing?
Bart
end

Test.module_eval do
# Object.const_missing, why not Test.const_missing?
Bart
end

I believe it's because of the pseudo-static way in which constants are
resolved. Spotted at parse-time, Bart is assumed to be a constant of
Object. If you change the first one to self.class::Bart, and the
second one to self::Bart, they will be resolved as Test::Bart.

David

--
David A. Black
dblack@wobblini.net

It's getting better:

···

-----------------------------------------------------------
def Object.const_missing(sym)
   puts "%s.const_missing: %s" % [self, sym]
end

class Test
   Bart = 42

   def Test.const_missing(sym)
     puts "%s.const_missing: %s" % [self, sym]
   end
end

Test.new.instance_eval do
   # Object.const_missing, why not Test.const_missing?
   Bart
end

Test.module_eval do
   # Object.const_missing, why not Test.const_missing?
   Bart
end

Test.module_eval "p Bart"
Test.new.instance_eval "p Bart"
-----------------------------------------------------------

output:

Object.const_missing: Bart
42

cheers

Simon