From: Joel VanderWerf Date: 2002-02-21T09:49:22+09:00 Subject: Re: confused about '::' vs '.' in method calls Raphael Clancy wrote: > > I must admit I'm a bit confused... probably because I never realy > understood the scope resoultion opperator when I learned C++, but I'm used > to seeing '::' when you want to show the scope of a method... Foo::Bar() > means Bar is a method in Foo called like so aFoo.Bar()... now in ruby, '::' > and '.' seem to be interchangeable... that is I can use both aFoo::Bar() > and aFoo.Bar() to call Bar on aFoo. In 'Ruby in a nutshell' Matz puts heavy > emphasis on using '::' for class methods, but the pick-axe book tends to > favor '.' Does it really matter which way you go? Are '::' and '.' > functionaly the same? Sorry if this is old hat, but I had a hard time > making google search the archive for '::' and '.' ;) > > Thanks! > > R. From the pickaxe chapter The Ruby Language: ===== When a receiver is explicitly specified in a method invocation, it may be separated from the method name using either a period ``.'' or two colons ``::''. The only difference between these two forms occurs if the method name starts with an uppercase letter. In this case, Ruby will assume that a receiver::Thing method call is actually an attempt to access a constant called Thing in the receiver unless the method invocation has a parameter list between parentheses. Foo.Bar() # method call Foo.Bar # method call Foo::Bar() # method call Foo::Bar # constant access ===== Something that didn't sink in for me right away is that the :: operator is dynamic in the first argument even when used for constant access. For example: irb(main):001:0> module M irb(main):002:1> K = 2 irb(main):003:1> end 2 irb(main):004:0> x = M M irb(main):005:0> x::K 2 But using the form 'x.K' is an error, because I never defined a method K.