From: Gavin Sinclair Date: 2003-09-14T15:04:49+09:00 Subject: Re: Dot versus double-colon On Sunday, September 14, 2003, 3:04:40 PM, Hal wrote: > OK, I've been thinking (always dangerous after 11 pm). > Class-level entities can be accessed via a double-colon. > No big mystery: > File::open(...) > File::SEPARATOR > Happily, a class method can also be accessed via a dot: > File.open(...) > But a constant can't: > File.SEPARATOR # syntax error > First, why is it this way? File.open() is sending the message :open to object File. The same cannot be said for the constant SEPARATOR. > Personally, I don't like the :: anyway, as it reminds me > of C++. I don't like :: for methods, because . makes sense for method calls on objects *and* classes, because .... well, you know why (*). :: makes sense for namespaces - as in constant resolution. It would not bother me one bit should :: be deprecated for method calls. > By the way, for those who are wondering, this *is* possible: > class File > def self.SEPARATOR > File::SEPARATOR > end > end > File.SEPARATOR # "/" > Even though it looks almost like recursion. Hmm, that raises > the question of why/how this works. I'm sure you realise now. It's because you've defined a method :) > And another issue is: One *could* write a little module that, > when included in a class, exposed all the class's constants in > this way. Within five minutes I had it "almost" working. I'll > bet someone in another timezone or with more caffeine could > have it working in three. Anyone? I'm happy for methods and constants to be accessed via different mechanisms (message passing vs namespace resolution) and for those mechanisms to be syntactically distinct. > Hal Cheers, Gavin (*) Classes *are* objects.