# Difference between :: and . in calling methods

**URL:** <https://www.ruby-forum.com/t/difference-between-and-in-calling-methods/58755>\
**Category:** Ruby\
**Created:** [April 25, 2006, 5:35pm UTC](https://www.ruby-forum.com/t/difference-between-and-in-calling-methods/58755 "2006-04-25T17:35:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mutejute](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@mutejute](https://www.ruby-forum.com/u/mutejute)\
**Post date:** [April 25, 2006, 5:35pm UTC](https://www.ruby-forum.com/t/difference-between-and-in-calling-methods/58755/1 "2006-04-25T17:35:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mutejute](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@mutejute](https://www.ruby-forum.com/u/mutejute)\
**Post date:** [April 25, 2006, 5:55pm UTC](https://www.ruby-forum.com/t/difference-between-and-in-calling-methods/58755/2 "2006-04-25T17:55:52Z")

</div>

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](http://www.rubypowerandlight.com))

“Ruby for Rails” PDF now on sale! [Ruby for Rails](http://www.manning.com/black)  
Paper version coming in early May!

---

<div class="post-metadata">

**Author:** ![mutejute](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@mutejute](https://www.ruby-forum.com/u/mutejute)\
**Post date:** [April 25, 2006, 6:20pm UTC](https://www.ruby-forum.com/t/difference-between-and-in-calling-methods/58755/3 "2006-04-25T18:20:27Z")

</div>

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](http://www.rubypowerandlight.com))

“Ruby for Rails” PDF now on sale! [Ruby for Rails](http://www.manning.com/black)  
Paper version coming in early May!

---

<div class="post-metadata">

**Author:** ![mutejute](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@mutejute](https://www.ruby-forum.com/u/mutejute)\
**Post date:** [April 26, 2006, 3:56am UTC](https://www.ruby-forum.com/t/difference-between-and-in-calling-methods/58755/4 "2006-04-26T03:56:19Z")

</div>

i understand now. many thanks.

---

<div class="post-metadata">

**Author:** ![mutejute](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@mutejute](https://www.ruby-forum.com/u/mutejute)\
**Post date:** [April 28, 2006, 5:03am UTC](https://www.ruby-forum.com/t/difference-between-and-in-calling-methods/58755/5 "2006-04-28T05:03:31Z")

</div>

In Message-Id: [removed\_email\_address@domain.invalid](mailto: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.
