# :: and

**URL:** <https://www.ruby-forum.com/t/and/119519>\
**Category:** Ruby\
**Created:** [November 7, 2007, 5:46pm UTC](https://www.ruby-forum.com/t/and/119519 "2007-11-07T17:46:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![joey\_eisma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a183cd/32.png) [@joey\_eisma](https://www.ruby-forum.com/u/joey_eisma)\
**Post date:** [November 7, 2007, 5:46pm UTC](https://www.ruby-forum.com/t/and/119519/1 "2007-11-07T17:46:26Z")

</div>

hi!

reading through some tutorial, i noticed the author would interchange  
using :: and . to call methods. i see they both give the same result.

i was wondering if there is any other difference between the two? (other  
than convenience maybe?). when would you use one over the other?

thanks!

joey

---

<div class="post-metadata">

**Author:** ![joey\_eisma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a183cd/32.png) [@joey\_eisma](https://www.ruby-forum.com/u/joey_eisma)\
**Post date:** [November 7, 2007, 6:36pm UTC](https://www.ruby-forum.com/t/and/119519/2 "2007-11-07T18:36:48Z")

</div>

They don’t both always give the same result. “.” is the message operator  
and  
“::” is the scoping operator. In the case of methods, they both work the  
same, but in the case of constants, you will get an “undefined method  
“CONSTANT”” if you use “.”.

So explicit scoping / getting nested constants, you have to use ::

For class methods, you can use either. I prefer “.”

Jason

---

<div class="post-metadata">

**Author:** ![joey\_eisma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a183cd/32.png) [@joey\_eisma](https://www.ruby-forum.com/u/joey_eisma)\
**Post date:** [November 7, 2007, 6:29pm UTC](https://www.ruby-forum.com/t/and/119519/3 "2007-11-07T18:29:12Z")

</div>

Hi –

On Thu, 8 Nov 2007, joey eisma wrote:

> hi!
> 
> reading through some tutorial, i noticed the author would interchange using :: and . to call methods. i see they both give the same result.
> 
> i was wondering if there is any other difference between the two? (other than convenience maybe?). when would you use one over the other?

I never use :: to call methods. Some people use it to call class  
methods, but I’ve never seen any reason to switch operators just  
because the receiver is a class or module.

David

---

<div class="post-metadata">

**Author:** ![joey\_eisma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a183cd/32.png) [@joey\_eisma](https://www.ruby-forum.com/u/joey_eisma)\
**Post date:** [November 7, 2007, 6:42pm UTC](https://www.ruby-forum.com/t/and/119519/4 "2007-11-07T18:42:05Z")

</div>

On Nov 7, 2007, at 9:45 AM, joey eisma wrote:

> thanks!  
> i occasionally use ‘::’ to get class values that are effectively  
> constants/config. for instance

if Monitor::interval \> 42

also, i tend to use ‘::’ if the class in question is stored in a  
variable, for example

c = method\_that\_returns\_a\_class

…

value = c::some\_method

simply because, when the code becomes complex, it helps my feeble  
mind recall that ‘c’ is indeed a class, and not a basic object.

mostly though i think this is a religious issue. fyi, you can  
interchange ‘.’ and ‘::’ for normal objects too

cfp:~ \> ruby -e’ p Object:🆕:object\_id ’  
76540

regards.

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![joey\_eisma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a183cd/32.png) [@joey\_eisma](https://www.ruby-forum.com/u/joey_eisma)\
**Post date:** [November 7, 2007, 6:39pm UTC](https://www.ruby-forum.com/t/and/119519/5 "2007-11-07T18:39:10Z")

</div>

joey eisma wrote:

> hi!
> 
> reading through some tutorial, i noticed the author would interchange  
> using :: and . to call methods. i see they both give the same result.
> 
> i was wondering if there is any other difference between the two?  
> (other than convenience maybe?). when would you use one over the  
> other?

It’s described somewhere in the pickaxe book. IIRC when you access a  
member of a module using ::, without trailing parentheses, then it’s  
considered accessing a constant. All other cases are method calls.

mortee

---

<div class="post-metadata">

**Author:** ![joey\_eisma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a183cd/32.png) [@joey\_eisma](https://www.ruby-forum.com/u/joey_eisma)\
**Post date:** [November 7, 2007, 7:14pm UTC](https://www.ruby-forum.com/t/and/119519/6 "2007-11-07T19:14:15Z")

</div>

joey eisma wrote:

> hi!
> 
> reading through some tutorial, i noticed the author would interchange using :: and . to call methods. i see they both give the same result.
> 
> i was wondering if there is any other difference between the two? (other than convenience maybe?). when would you use one over the other?

In addition to the other answers, there’s one place that you can only  
use ‘::’ rather than ‘.’, and that’s when you’re specifying that your  
constant should be searched for in the root namespace:

Bar = 42  
module Foo  
Bar = 23  
def Foo.show  
p ::Bar  
p Bar  
end  
end

Foo.show outputs:  
42  
23

---

<div class="post-metadata">

**Author:** ![joey\_eisma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a183cd/32.png) [@joey\_eisma](https://www.ruby-forum.com/u/joey_eisma)\
**Post date:** [November 7, 2007, 6:55pm UTC](https://www.ruby-forum.com/t/and/119519/7 "2007-11-07T18:55:32Z")

</div>

mortee wrote:

> It’s described somewhere in the pickaxe book. IIRC when you access a  
> member of a module using ::, without trailing parentheses, then it’s  
> considered accessing a constant. All other cases are method calls.

Ah yes, after seeing the other posts in this thread, I have to add that  
it only applies if the name after the :: starts with a capital letter.  
Of course you can’t have constants starting with lowercase letters, so

MyModule::some\_member

is also a method call.

mortee
