# What is the difference between :, ::, .?

**URL:** <https://www.ruby-forum.com/t/what-is-the-difference-between/112812>\
**Category:** Ruby\
**Created:** [August 23, 2007, 2:32pm UTC](https://www.ruby-forum.com/t/what-is-the-difference-between/112812 "2007-08-23T14:32:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mobileamusement](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mobileamusement](https://www.ruby-forum.com/u/mobileamusement)\
**Post date:** [August 23, 2007, 2:32pm UTC](https://www.ruby-forum.com/t/what-is-the-difference-between/112812/1 "2007-08-23T14:32:32Z")

</div>

Hi, I’m still confused when to use :: or : or . to access namespaces  
and methods. Could someone please tell me concisely when each should  
be used?

Many thanks

Gabriel

---

<div class="post-metadata">

**Author:** ![mobileamusement](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mobileamusement](https://www.ruby-forum.com/u/mobileamusement)\
**Post date:** [August 23, 2007, 2:57pm UTC](https://www.ruby-forum.com/t/what-is-the-difference-between/112812/2 "2007-08-23T14:57:36Z")

</div>

Alle giovedì 23 agosto 2007, Gabriel D. ha scritto:

> Hi, I’m still confused when to use :: or : or . to access namespaces  
> and methods. Could someone please tell me concisely when each should  
> be used?
> 
> Many thanks
> 
> Gabriel

- The dot (.) is used to access a method of an object (remember that  
class/module methods are simply instance methods of the class object).  
For  
example:

- The double colon (:🙂 operator is used to access a constant contained  
in a  
module or class (remember that classes and modules are usually stored in  
constants). It can also be used to access class/module methods (but I  
think  
this is not much used)

- The colon (🙂 operator has nothing to do with namespaces. Its only  
use, as  
far as I remember, is in the ternary operator ?

which is equivalent to

if a then b  
else c  
end

Besides, a colon is also used in Symbol literals:

:a\_symbol

which is the same as ‘a\_symbol’.to\_sym.

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![mobileamusement](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mobileamusement](https://www.ruby-forum.com/u/mobileamusement)\
**Post date:** [August 23, 2007, 3:31pm UTC](https://www.ruby-forum.com/t/what-is-the-difference-between/112812/3 "2007-08-23T15:31:40Z")

</div>

Hi,

Am Donnerstag, 23. Aug 2007, 21:30:46 +0900 schrieb Gabriel D.:

> I’m still confused when to use :: or : or . to access namespaces and  
> methods. Could someone please tell me concisely when each should be used?

[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/251018](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/251018)

Bertram

---

<div class="post-metadata">

**Author:** ![mobileamusement](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mobileamusement](https://www.ruby-forum.com/u/mobileamusement)\
**Post date:** [August 23, 2007, 3:58pm UTC](https://www.ruby-forum.com/t/what-is-the-difference-between/112812/4 "2007-08-23T15:58:45Z")

</div>

On Aug 23, 2007, at 2:56 PM, Stefano C. wrote:

> (remember that classes and modules are usually stored in  
> constants).

Are there exceptions?

---

<div class="post-metadata">

**Author:** ![mobileamusement](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mobileamusement](https://www.ruby-forum.com/u/mobileamusement)\
**Post date:** [August 23, 2007, 4:19pm UTC](https://www.ruby-forum.com/t/what-is-the-difference-between/112812/5 "2007-08-23T16:19:49Z")

</div>

Alle giovedì 23 agosto 2007, Xavier N. ha scritto:

> On Aug 23, 2007, at 2:56 PM, Stefano C. wrote:
> 
> > (remember that classes and modules are usually stored in  
> > constants).
> 
> Are there exceptions?

You can store a class object in any variable. For example:

- in a global variable

$cls = Hash  
$cls.new  
=\> {}

- in an instance variable (the point of the example is @cls, not C, of  
course)

- in a local variable:

I think that the code

class MyClass  
end

does two different things: 1) creates an instance of class Class with  
name  
MyClass; 2) stores this instance in the constant MyClass.

If you call Class.new, you get an anonymous class object which is not  
stored  
in a constant.

Another interesting example is Struct.new. It returns a class object  
corresponding to the new class, but it doesn’t get stored in a constant,  
unless you pass a name to Struct.new or explicitly put it in a constant:

cls = Struct.new :attr1, :attr2  
=\> #Class:0xb79cfb8c #the class is only stored in the local variable  
cls  
Cls = Struct.new :attr1, :attr2  
=\> Cls #now the class is stored in the constant S  
cls = Struct.new ‘Cls’, :attr1, :attr2  
=\> Struct::Cls #now the class is stored in the constant Struct::Cls

Stefano

---

<div class="post-metadata">

**Author:** ![mobileamusement](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mobileamusement](https://www.ruby-forum.com/u/mobileamusement)\
**Post date:** [August 23, 2007, 5:43pm UTC](https://www.ruby-forum.com/t/what-is-the-difference-between/112812/6 "2007-08-23T17:43:01Z")

</div>

On Aug 23, 2007, at 4:19 PM, Stefano C. wrote:

> Alle giovedì 23 agosto 2007, Xavier N. ha scritto:
> 
> > On Aug 23, 2007, at 2:56 PM, Stefano C. wrote:
> > 
> > > (remember that classes and modules are usually stored in  
> > > constants).
> > 
> > Are there exceptions?
> 
> You can store a class object in any variable. For example:

Oh yes, classes are objects.

What I actually wondered (but wording was bad) was whether you can  
have a named class without assigning to a constant. This was a slip  
while reading because the post mentioned “storage”, which is more  
generic and correct of course.

If I understand correctly the way this works non-anonynous classes  
are just class objects assigned to regular constants. That is

class C  
…  
end

is approximately

C = Class.new  
C.class\_eval do  
…  
end

so to speak (I don’t claim it is equivalent because I’ve not  
analized all the implications). I came accross this when I tried to  
understand Rails class reloading: you remove the constants and that  
triggers const\_missing again, which assigns a new definition to that  
constant. The code in the interpreter looks up the constant, if it  
exists the class is reopened, if it doesn’t the class is defined.

I think that relationship between classes and constants is beautiful,  
they are formally decoupled (of course with some coupling in practice  
for convenience), and is yet another example of the consequences of  
chosing a few core concepts and build on top of them.

– fxn

---

<div class="post-metadata">

**Author:** ![mobileamusement](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mobileamusement](https://www.ruby-forum.com/u/mobileamusement)\
**Post date:** [August 24, 2007, 10:29am UTC](https://www.ruby-forum.com/t/what-is-the-difference-between/112812/7 "2007-08-24T10:29:11Z")

</div>

> Am Donnerstag, 23. Aug 2007, 21:30:46 +0900 schrieb Gabriel D.:
> 
> > I’m still confused when to use :: or : or . to access  
> > namespaces and  
> > methods. Could someone please tell me concisely when each  
> > should be used?
> 
> [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/251018](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/251018)

Hmmmm … I thought I had already understand this issue (use  
CLASSNAME:: for accessing constants, and CLASSNAME. for accessing  
class methods), but then I fail to understand why both

File::delete(“filename”)  
File.delete(“filename”)

work. Only the second form should be correct then, shouldn’t it?

Ronald
