From: dblack@... Date: 2006-03-27T01:56:11+09:00 Subject: Re: Noob: use of the double colon Hi -- On Mon, 27 Mar 2006, ara.t.howard@noaa.gov wrote: > On Mon, 27 Mar 2006 dblack@wobblini.net wrote: > >> I honestly don't see any connection between the two techniques (:: and >> suggestive variable names). c::meth doesn't communicate anything to me >> about c. > > it does though - you know c __must__ be a module (classes being modules). Why? c = "abc" c::split(//) # ["a", "b", "c"] >> Even if it did, I'm not sold on the idea that it's necessary to have a >> different message-sending operator for a particular class of objects. It >> doesn't scale: you can come up with lots of variable names (my_class, >> etc.), >> but you can't keep adding operators to make distinctions among receivers. >> Nor is it necessarily vital in very many cases to make such distinctions. > > i can see that point of view. still, i like this: > > harp:~ > cat a.rb > module A > module B > module C > class D; end > E = 42 > def self::foo() 42 end > end > end > end > > namespace = A::B::C > > p namespace::D > p namespace::E > p namespace::foo > > > harp:~ > ruby a.rb > A::B::C::D > 42 > 42 > > and i think it's why '::' can call methods - though this is a w.a.g. To each his own. My brain has to parse "namespace::foo" twice: the first time it says, "Didn't he mean namespace::Foo?" and the second time it says, "Oh right, Ruby's superfluous message-sending operator" :-) >> And when it's a matter of String.new vs. String::new, you already know it's >> a class anyway :-) > > only sometimes though: > > > module M > class C > end > end > > def C(*a, &b) ::M::C::new(*a, &b) end > > C.new > > > this is contrived, but String is a perfect example of this: both String() and > String exist and certainly many libs export 'const methods'. > > all you really when a variable starts with [A-Z] is that it's a const. this > happends quite a bit in my code because i always wrap up code into modules so > i > end up with things like this: > > module M > module Logging > end > module Util > end > class A > include Util > include Logging > end > class B > include Util > include Logging > end > class C > include Util > include Logging > end > def self::new *a, &b > C::new *a, &b > end > end > > where 'C' is sort of the 'default' or 'main' class in this set - so i provide > a convenience method for the common ctor case. of course you can use '.new' > here... i'm just pointing out that a lefthand side const may not be a class. Absolutely -- indeed, it can be anything. (My String example was meant to evoke the specific case of a well-know class.) That's why I don't find it informative. I don't know; I guess I just see the notion of "sending a message to an object" fully covered, on the operator side (as opposed to the "send" side), by the dot, and no clear rationale for Ruby having two such operators. David -- David A. Black (dblack@wobblini.net) Ruby Power and Light, LLC (http://www.rubypowerandlight.com) "Ruby for Rails" chapters now available from Manning Early Access Program! http://www.manning.com/books/black