From: "Mauricio Fernández" Date: 2003-09-14T17:48:12+09:00 Subject: Re: Dot versus double-colon 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. > >:: 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? I agree w/ Gavin here, I'd be happy to drop the SomeClass::singleton_method notation. > >>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? The constant takes precedence over the method when using ::, so no recursion. > >>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? 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 batsman@tux-chan:/tmp$ ruby i.rb "/" Using method_missing has the following properties: * works for constants defined after .extend * it's much slower than defining methods for the existent constants (that's easy to write too). > >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. > > I see what you mean. But I say: Why should a constant be anything > but a reader which happens always to return the same value? Speed? It isn't probably the primary reason, however. Another benefit is that Ruby can give you a better warning if you redefine a constant (you'll get warning: method redefined; discarding old Constant in the other case). -- _ _ | |__ __ _| |_ ___ _ __ ___ __ _ _ __ | '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \ | |_) | (_| | |_\__ \ | | | | | (_| | | | | |_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_| Running Debian GNU/Linux Sid (unstable) batsman dot geo at yahoo dot com We come to bury DOS, not to praise it. -- Paul Vojta, vojta@math.berkeley.edu