T. Onoma
9/30/2004 12:05:00 AM
For reference:
>
> class Object
> def const_get(c)
> self.class.const_get(c)
> end
> def const_set(c,v)
> self.class.const_set(c,v)
> end
> end
>
> module Kernel
> alias method_missing_orig method_missing
> def method_missing(m,*a,&b)
> Class === (c = const_get(m)) ? c::new(*a,&b) :
> method_missing_orig(m,*a,&b)
> end
> end
David wrote:
> I've lost track of where this is going a bit, but here you seem to be
> making it look like non-module/class objects have constants:
>
>Â a = ""
>Â a.const_set("X",1)
>Â p a.const_get("X") Â # 1
>
> not to mention:
>
>Â p String::X Â # 1
>
>(i.e., indirect setting of a Class object's constant). Â
Okay, perhaps they should be private. Beyond that, I'm not sure its really a
problem since one can just as easily do:
self.class.const_set
Yes, this makes it clear that they are class level entities. But the former is
polymorphic. Perhaps you can show a good use case for why the former is not
good to have?
But the above issue aside, this was just the solution I next derived. I'm sure
there is a more appropriate way to code this method_missing handler --namely
keeping it in-line with the proper module namespace rules. Perhaps this is
better:
class Module
def method_missing(m,*a,&b)
Class === (c = const_get(m)) ? c::new(*a,&b) : super
end
end
class Object
def method_missing(m,*a,&b)
Class === (c = self.class.const_get(m)) ? c::new(*a,&b) : super
end
end
Better suggestions?
T.