[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Gabriel Dragffy

8/23/2007 12:31:00 PM

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

6 Answers

Stefano Crocco

8/23/2007 12:57:00 PM

0

Alle giovedì 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
class/module methods are simply instance methods of the class object). For
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
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)

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, as
far as I remember, is in the ternary operator ?

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

Bertram Scharpf

8/23/2007 1:30:00 PM

0

Hi,

Am Donnerstag, 23. Aug 2007, 21:30:46 +0900 schrieb Gabriel Dragffy:
> 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/...

Bertram

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...

Xavier Noria

8/23/2007 1:58:00 PM

0

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

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

Are there exceptions?

Stefano Crocco

8/23/2007 2:19:00 PM

0

Alle giovedì 23 agosto 2007, Xavier Noria ha scritto:
> On Aug 23, 2007, at 2:56 PM, Stefano Crocco 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)

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

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

* in a local variable:

cls = String
cls.new
=> ''

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

Xavier Noria

8/23/2007 3:42:00 PM

0

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

> Alle giovedì 23 agosto 2007, Xavier Noria ha scritto:
>> On Aug 23, 2007, at 2:56 PM, Stefano Crocco 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


Ronald Fischer

8/24/2007 8:27:00 AM

0

> Am Donnerstag, 23. Aug 2007, 21:30:46 +0900 schrieb Gabriel Dragffy:
> > 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/...

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
--
Ronald Fischer <ronald.fischer@venyon.com>
Phone: +49-89-452133-162