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

G

Gabriel Dragffy

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
 
S

Stefano Crocco

Alle gioved=EC 23 agosto 2007, Gabriel Dragffy 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=20
class/module methods are simply instance methods of the class object). For=
=20
example:

"a string".capitalize
1.ceil
[1, 2, 3].uniq
Regexp.escape

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

Regexp::EXTENDED #a constant in the Regexp class
Enumerable::Enumerator #a class in the Enumerable modules
File::exist? #a class method of the File class

* The colon :)) operator has nothing to do with namespaces. Its only use, a=
s=20
far as I remember, is in the ternary operator ?
=20
a ? b : c

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
 
S

Stefano Crocco

Alle gioved=EC 23 agosto 2007, Xavier Noria ha scritto:
Are there exceptions?

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

* in a global variable

$cls =3D Hash
$cls.new
=3D> {}

* in an instance variable (the point of the example is @cls, not C, of cour=
se)

class C
attr_reader :cls
def initialize cls
@cls =3D cls
end
end

c =3D C.new Array
c.cls.new
=3D> []

* in a local variable:

cls =3D String
cls.new
=3D> ''

I think that the code

class MyClass
end

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

If you call Class.new, you get an anonymous class object which is not store=
d=20
in a constant.

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

cls =3D Struct.new :attr1, :attr2
=3D> #<Class:0xb79cfb8c> #the class is only stored in the local variable cls
Cls =3D Struct.new :attr1, :attr2=20
=3D> Cls #now the class is stored in the constant S
cls =3D Struct.new 'Cls', :attr1, :attr2=20
=3D> Struct::Cls #now the class is stored in the constant Struct::Cls

Stefano
 
X

Xavier Noria

Alle gioved=EC 23 agosto 2007, Xavier Noria ha scritto:

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 =20
have a named class without assigning to a constant. This was a slip =20
while reading because the post mentioned "storage", which is more =20
generic and correct of course.

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

class C
...
end

is approximately

C =3D Class.new
C.class_eval do
...
end

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

I think that relationship between classes and constants is beautiful, =20=

they are formally decoupled (of course with some coupling in practice =20=

for convenience), and is yet another example of the consequences of =20
chosing a few core concepts and build on top of them.

-- fxn
 
R

Ronald Fischer

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

Hmmmm ... I thought I had already understand this issue (use=20
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
--=20
Ronald Fischer <[email protected]>
Phone: +49-89-452133-162
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,264
Messages
2,571,316
Members
48,002
Latest member
DoloresMan

Latest Threads

Top