Const_missing

I just tried to use const_missing (which I think is a
neat idea).

Shouldn’t it work at the top level? Am I doing something
wrong?

def const_missing
"not found"
end

p FOO

The above gives an error.

Hal

···


Hal Fulton
hal9000@hypermetrics.com

Hal E. Fulton wrote:

I just tried to use const_missing (which I think is a
neat idea).

Shouldn’t it work at the top level? Am I doing something
wrong?

def const_missing
“not found”
end

p FOO

The above gives an error.

I guess #const_missing gets sent to the class, not to the instance,
since constants belong to the class. So make it a class method:

irb(main):001:0> def Object::const_missing(c); puts “missing: #{c}”; end
=> nil
irb(main):002:0> AAA
missing: AAA
=> nil

Hal E. Fulton wrote:

I just tried to use const_missing (which I think is a
neat idea).

Shouldn’t it work at the top level? Am I doing something
wrong?

def const_missing
“not found”
end

This should be

···

def Object.const_missing(sym)
p sym
end

Foo

/Christoph

Hal E. Fulton wrote:

The above gives an error.

On second thought your implementation could
have worked - the following may sheet more light
(confusion in my case;-) on it …

···


def const_missing(sym)
p sym
end

def const_missingx(sym)
p sym
end

public :const_missing,:const_missingx
Object.const_missingx(:K) # :K
Object.const_missing(:K) # throws a NameError ???

/Christoph

Right, thanks. I also forgot the parameter.

Hal

···

----- Original Message -----
From: “Joel VanderWerf” vjoel@PATH.Berkeley.EDU
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Thursday, August 21, 2003 1:58 PM
Subject: Re: const_missing

I guess #const_missing gets sent to the class, not to the instance,
since constants belong to the class. So make it a class method:

irb(main):001:0> def Object::const_missing(c); puts “missing: #{c}”; end
=> nil
irb(main):002:0> AAA
missing: AAA
=> nil


Hal Fulton
hal9000@hypermetrics.com