From: Gavin Sinclair Date: 2003-09-14T17:05:29+09:00 Subject: Re: Dot versus double-colon On Sunday, September 14, 2003, 5:13:04 PM, Hal wrote: > Gavin Sinclair wrote: >>>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. > Certainly it could be said. In fact, there is no way to tell > by looking at it whether it is in fact a method or a constant. It "could" be said, but it can't. The interpreter knows whether it's a constant, and the capital letter is more than just a convention as well - even though SEPERATOR is a valid method name. > And a constant could be viewed as a method that always returns > the same value. Uniform access, you know. Yes, it's appealing. Sometimes I implement "constants" as methods, because I want the return value to be regenerated each time. If a constant is a hash, its innards can be modified, and it ain't so constant anymore. I suppose there's :freeze... So your statement above should be qualified: the constant would be a method that always returns the same object, not neccesarily the same value. >> :: makes sense for namespaces - as in constant resolution. It would >> not bother me one bit should :: be deprecated for method calls. > I'm accessing a value within an object. Sure, the value > happens to be constant. So what? Sending a message is very different from accessing a value. I acknowledge your intentions, though I'm happy the way things are, but that fundamental needs to be kept in mind, IMO. Just like attr_* are convenient sugar for literal accessor methods, are you proposing that "CONSTANT = 1" could/should be sugar for creating a method? >> >>>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 :) > You missed my point. Since the method File.SEPARATOR can be referred > to by the syntax File::SEPARATOR, then how does the method know that > it is not calling itself recursively? Presumably the interpreter looks for a constant before a method. This may be good luck rather than good management, although the :: may have something to do with it. > [...] Cheers, Gavin BTW, you could make constants available through dot-notation using :method_missing, couldn't you?