From: Hal Fulton Date: 2003-09-14T18:01:47+09:00 Subject: Re: Dot versus double-colon Mauricio Fern�ndez wrote: > On Sun, Sep 14, 2003 at 04:13:04PM +0900, Hal Fulton wrote: > >>And a constant could be viewed as a method that always returns >>the same value. Uniform access, you know. > > > The problem you identified (infinite recursion) justifies the existence > of ::. Moreover, :: should be somewhat faster than a normal method > dispatch. Well, I guess I'm just being a little irrational here. I just happen to dislike :: in every case. I think what I'm saying is not "make constants into methods" but "make the notations the same (and disallow methods named the same as constants)." But it's unimportant and there are probably reasons for not doing so. > The constant takes precedence over the method when using ::, so no > recursion. Hmm. So if you *wanted* to recurse, you'd have to use the dot notation. Again it's only an issue because Ruby allows a class to have a method and a constant by the same name. Here's another thought question. Are all constants class-level? Would a singleton constant attached to an object make sense? > There's a really obvious possible solution: > > batsman@tux-chan:/tmp$ expand -t2 i.rb > > if nil > > class Module > alias_method :_old_method_missing, :method_missing > def method_missing(meth, *args, &block) > return const_get(meth) if const_defined? meth > _old_method_missing > end > end > > end > > # or > > module ExposeConstants > def self.extend_object(mod) > class << mod; > alias_method :_old_method_missing, :method_missing > end > super > end > > def method_missing(meth, *args, &block) > return const_get(meth) if const_defined? meth > _old_method_missing > end > end > > File.extend ExposeConstants > > p File.SEPARATOR Very good. I'd have seen that if I hadn't failed to see it. How's that for a tautology? Hal