Obtaining object in const_missing

T.,

I'm stumped. Is there any way to do this?

class Object
   def self.const_missing(name)
     p self # => X
     # Okay, but how do I get at object x?
     x = ?
     if x.respond_to?(:myconstant)
        return "Gotchya"
     else
        raise # proper error?
     end
   end
end

class X
   def myconstant
     puts NewConstant
   end
end

x = X.new
x.myconstant

    As our own comment points out, self is class X, not the instance of
class X currently pointed at by the variable x. What you want to do is
call Module#method_defined? instead of Object#respond_to?, so your if
statement becomes:

    if method_defined?(:myconstant)

    I hope this helps.

    - Warren Brown

Thanks, Warren. That *almost* helps :wink: Unfortunately I am needing to access
another part of x (namely #method). Any ideas? I've been thinking on it for a
while now. I'm getting the feeling there is no way... if that's so, I think
it might be my first RCR that has a chance in hell of getting through! :))

···

On Thursday 19 August 2004 05:21 pm, Warren Brown wrote:

    As our own comment points out, self is class X, not the instance of
class X currently pointed at by the variable x. What you want to do is
call Module#method_defined? instead of Object#respond_to?, so your if
statement becomes:

    if method_defined?(:myconstant)

    I hope this helps.

--
T.