Forum: Ruby difference between :: and . in calling methods

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Malamute J. (Guest)
on 2006-04-25 19:35
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.
unknown (Guest)
on 2006-04-25 19:55
(Received via mailing list)
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 (removed_email_address@domain.invalid)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" PDF now on sale!  http://www.manning.com/black
Paper version coming in early May!
unknown (Guest)
on 2006-04-25 20:20
(Received via mailing list)
Hi --

On Wed, 26 Apr 2006, removed_email_address@domain.invalid 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 (removed_email_address@domain.invalid)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" PDF now on sale!  http://www.manning.com/black
Paper version coming in early May!
Malamute J. (Guest)
on 2006-04-26 05:56
i understand now. many thanks.
YANAGAWA Kazuhisa (Guest)
on 2006-04-28 07:03
(Received via mailing list)
In Message-Id: <removed_email_address@domain.invalid>
removed_email_address@domain.invalid 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.
This topic is locked and can not be replied to.