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.

···

--
Posted via http://www.ruby-forum.com/.

Hi --

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 . ?

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

···

On Wed, 26 Apr 2006, malamute jute wrote:

--
David A. Black (dblack@wobblini.net)
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 --

Hi --

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 . ?

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.

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

···

On Wed, 26 Apr 2006, dblack@wobblini.net wrote:

On Wed, 26 Apr 2006, malamute jute wrote:

--
David A. Black (dblack@wobblini.net)
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.

···

--
Posted via http://www.ruby-forum.com/.

In Message-Id: <Pine.LNX.4.64.0604251216530.25678@rubypal.com>
dblack@wobblini.net 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.

···

--
kjana@dm4lab.to April 28, 2006
What can't be cured must be endured.