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
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
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:
“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
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
Bertram
On Aug 23, 2007, at 2:56 PM, Stefano C. wrote:
(remember that classes and modules are usually stored in
constants).
Are there exceptions?
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:
$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
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
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
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs