Obtaining object in const_missing

T

T. Sawyer

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.
 
D

David A. Black

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
 
T

T. Onoma

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.

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.
 
T

T. Onoma

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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top