# Use of Double-Colon ::

**URL:** <https://www.ruby-forum.com/t/use-of-double-colon/103741>\
**Category:** Ruby\
**Created:** [May 9, 2007, 8:40pm UTC](https://www.ruby-forum.com/t/use-of-double-colon/103741 "2007-05-09T20:40:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 9, 2007, 8:40pm UTC](https://www.ruby-forum.com/t/use-of-double-colon/103741/1 "2007-05-09T20:40:36Z")

</div>

Hi,

I’m new to Ruby and throughly enjoying learning the language. I’m  
currently working my way through the Pickaxe book, but have a couple  
of questions I hope someone can help me out with.

As I understand it the double-colon ( :: ) can be used when calling  
class methods or class constants, but could correctly be replaced by a  
single dot ( . ) if preferred:

SongList::is\_too\_long(song1)  
SongList.is\_too\_long(song1)

I was wondering if someone more experience could tell me which method  
is used by the majority of Rubyists and whether it is thought of as  
“best practice” to use the double colon?

I suspect this is probably down to personal preference. I personally  
prefer the double-colon but I would like to get into good habits now  
whilst I’m learning the language. The Pickaxe book doesn’t use ::  
except when referring to modules (not sure if that is the correct  
term), e.g.,

class TestRoman \< Test::Unit::TestCase  
#…  
end

Could someone explain whether this is convention or if there is  
another reason for this?

Many thanks!  
Robin

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 9, 2007, 8:53pm UTC](https://www.ruby-forum.com/t/use-of-double-colon/103741/2 "2007-05-09T20:53:37Z")

</div>

Alle mercoledì 9 maggio 2007, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) ha scritto:

> SongList::is\_too\_long(song1)  
> term), e.g.,
> 
> class TestRoman \< Test::Unit::TestCase  
> #…  
> end
> 
> Could someone explain whether this is convention or if there is  
> another reason for this?
> 
> Many thanks!  
> Robin

You need to use the double column when referring to a constant. You can  
use  
either the double column or the dot when calling a method:

class C  
CONST=1  
def C.a\_method  
puts "a\_method  
end  
end

C::a\_method  
=\> “a\_method”

C.a\_method  
=\> “a\_method”

puts C::CONST  
=\> 1

puts C.CONST  
=\> NoMetodError

In the last case, ruby is trying to find a class method of C named CONST  
(because the dot can only be used to call a method). Since such a method  
doesn’t exist, a NoMethodError exception is raised. In the example of  
class  
TestRoman, the double columns can’t be replaced by dots because TestCase  
and  
Unit are constants (respectively, a constant referring to a Module  
declared  
in the Test module and a constant referring to a Class declared in the  
Unit  
module).

As for which form is most used, I’m not an expert, but I think that the  
dot is  
most used to call class methods.

Stefano

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 9, 2007, 8:54pm UTC](https://www.ruby-forum.com/t/use-of-double-colon/103741/3 "2007-05-09T20:54:37Z")

</div>

On Wednesday 09 May 2007 18:40, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> SongList::is\_too\_long(song1)  
> term), e.g.,
> 
> class TestRoman \< Test::Unit::TestCase  
> #…  
> end
> 
> Could someone explain whether this is convention or if there is  
> another reason for this?
> 
> Many thanks!  
> Robin

If i remember correctly you use :: for Classes, constants etc and . for  
methods, it was here before - read archives first

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 9, 2007, 9:16pm UTC](https://www.ruby-forum.com/t/use-of-double-colon/103741/4 "2007-05-09T21:16:41Z")

</div>

Hi –

On Thu, 10 May 2007, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> SongList::is\_too\_long(song1)  
> term), e.g.,
> 
> class TestRoman \< Test::Unit::TestCase  
> #…  
> end
> 
> Could someone explain whether this is convention or if there is  
> another reason for this?

I think it’s clearer just to use a dot to mean “send this to the  
object”, and :: to drill down constant scope. I don’t think there’s  
any reason to use a different operator just because the receiver is a  
class or module. You’re still doing the same thing: sending a message  
to an object. So I’d just use the dot.

David

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 9, 2007, 9:37pm UTC](https://www.ruby-forum.com/t/use-of-double-colon/103741/5 "2007-05-09T21:37:43Z")

</div>

On 9 May, 20:15, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Could someone explain whether this is convention or if there is  
> > –  
> > Q. What is THE Ruby book for Rails developers?  
> > A. RUBY FOR RAILS by David A. Black ([Ruby for Rails - David A. Black | Manning](http://www.manning.com/black))  
> > (See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf](http://www.rubypal.com/r4rrevs.pdf))  
> > Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
> > A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

Thanks everyone. That all makes sense.

I first met double-colon in the Class methods section of “why’s guide  
to ruby” ( [A Quick (and Hopefully Painless) Ride Through Ruby (with Cartoon Foxes) | Why's (Poignant) Guide to Ruby](http://poignantguide.net/ruby/chapter-3.html) ), it doesn’t  
mention using dot as an alternative in that section so I assumed  
double-colon was the standard. Thanks for clearing it up.

Robin

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 10, 2007, 12:07pm UTC](https://www.ruby-forum.com/t/use-of-double-colon/103741/6 "2007-05-10T12:07:51Z")

</div>

Hi,

Am Donnerstag, 10. Mai 2007, 03:52:55 +0900 schrieb Marcin R.:

> On Wednesday 09 May 2007 18:40, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > As I understand it the double-colon ( :: ) can be used when calling  
> > class methods or class constants, but could correctly be replaced by a  
> > single dot ( . ) if preferred:
> 
> If i remember correctly you use :: for Classes, constants etc and . for  
> methods, it was here before - read archives first

Classes are mostly referred to by constants

class C  
X = “x”  
class \<\< self  
def X ; “y” ; end  
end  
end

c = C # C is a constant

cobj = c.new

c::X # =\> “x”  
c.X # =\> “y”  
C::X # =\> “x”  
C.X # =\> “y”

Bertram

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 9, 2007, 9:03pm UTC](https://www.ruby-forum.com/t/use-of-double-colon/103741/7 "2007-05-09T21:03:39Z")

</div>

On May 9, 2007, at 2:40 PM, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Could someone explain whether this is convention or if there is  
> another reason for this?

As the other responders have pointed out, you have to use :: when  
you are accessing constants within modules. And since classes  
are modules, the same applies for classes.

While both :: and . work for calling methods, I prefer . because  
it emphasizes the idea that a module (or a class) is _just_  
another object and isn’t ‘special’ with respect to method  
invocation.

Gary W.
