Obtaining object in const_missing

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

Thanks,
T.

Hi --

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

Sort of thinking it through aloud, so to speak:

I think the problem might be that x has no special status inside
const_missing. Consider that you could also do:

  class Y
    def otherconstant
      puts X::NewConstant
    end
  end

  y = Y.new
  y.otherconstant

and then y would be playing the role that x is playing in your example.

So it becomes a pretty general role: the object that received a
message that resulted in a method call during the course of which some
class or module's missing constant was referenced. I'm not sure that
much history and context is really the business of the class or module
whose constant it is.

David

···

On Fri, 20 Aug 2004, T. Sawyer wrote:

--
David A. Black
dblack@wobblini.net

Unless that constant needs to be lazily set to an object in that context. In
other words, the Constant itself is being called from that context, so its
value ought to have the potential of being from that context as well.
Shouldn't it? If it weren't for the auto-instantiation that I require, I
could just as easily define a constant manually in that context.

So, while I understand the inclination of thought you present, I do not think
that such a "business" barrier is truly warrented.

T.

···

On Thursday 19 August 2004 06:11 pm, David A. Black wrote:

So it becomes a pretty general role: the object that received a
message that resulted in a method call during the course of which some
class or module's missing constant was referenced. I'm not sure that
much history and context is really the business of the class or module
whose constant it is.

I looked at the source code real quick (not that I'm anywhere near qualified).
But I did notice that in eval.c:
  
  ev_const_get(cref, id, self)

So here self is taken in, it's just that subsequently it is discarded in the
call to rb_const_get and the like.

Just trying to get a feel for feasibility.

···

On Friday 20 August 2004 12:00 am, T. Onoma wrote:

In other words, the Constant itself is being called from that context, so
its value ought to have the potential of being from that context as well.
Shouldn't it? If it weren't for the auto-instantiation that I require, I
could just as easily define a constant manually in that context.

--
T.