Difference between :: and . in calling methods

newb here.

whats the difference between :: and . in calling methods? like for
example:

File::open and File.open

As I understand, you use :: in invoking the constant of a class like
Math::PI .

Another thing is if i type these in IRB, it returns the same result:

Object::constants
Object.constants

But if i type this, i get undefined method error

Object.constants.Numeric

whereas this is ok:

Object::constants::Numeric

So the question now is when do I use the :: and when do I use the . ?

Many thanks.

Hi –

On Wed, 26 Apr 2006, malamute jute wrote:

Another thing is if i type these in IRB, it returns the same result:
Object::constants::Numeric

So the question now is when do I use the :: and when do I use the . ?

The :: is never used – really, I’ve literally never seen it – except
when the receiver is a class or module. In such cases, it’s sometimes
used, though I’ve never understood why, as it makes much more sense to
me just to do:

receiver.message

regardless of the class of the receiver.

I would love to see :: removed, except for its other purpose of
constant lookup. (It also occasionally, and understandably, causes
confusion because of that dual role.)

David


David A. Black ([email protected])
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

“Ruby for Rails” PDF now on sale! Ruby for Rails
Paper version coming in early May!

Hi –

On Wed, 26 Apr 2006, [email protected] wrote:

Object.constants.Numeric
when the receiver is a class or module. In such cases, it’s sometimes
used, though I’ve never understood why, as it makes much more sense to
me just to do:

receiver.message

regardless of the class of the receiver.

Let me clarify that:

:: as a method-call operator is never used, except with class/module
receivers.

:: is also the (one and only) constant path separator. That’s why
.Numeric doesn’t work; Numeric isn’t a method.

The dual role of :: is what led to your confusion in that example, as
it has for many people.

David


David A. Black ([email protected])
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

“Ruby for Rails” PDF now on sale! Ruby for Rails
Paper version coming in early May!

i understand now. many thanks.

In Message-Id: [email protected]
[email protected] writes:

Let me clarify that:

:: as a method-call operator is never used, except with class/module
receivers.

:: is also the (one and only) constant path separator. That’s why
Numeric doesn’t work; Numeric isn’t a method.

The dual role of :: is what led to your confusion in that example, as
it has for many people.

Note: “::” notation for a method invocation was introduced since
someone wanted to call a “class method” in that fashion like as in C++
or Java, and Matz agreed — or at least didn’t rejected. On the
other hand “.” notation is core syntax for a method invocation before
that.

“::” for a constant path separator was older than “::” for methods

too.

I personally prefer “.” for all method invocations because I’m too
lazy to remember which method should be called which, and feel using
“.” is reflecting the model that some class is an instance of class
Class.