Meaning of ::

  • Thread starter Guido Granobles
  • Start date
G

Guido Granobles

Hi!.. I'm very new with Ruby and I wold like to know what does it mean
the four points when I am doing inheritance. Like:
Code:
class1::base
I know that it represents a constant but how I should interpret that.
 
S

Scott Gonyea

Class and Module names must be in their "proper case" form. So, =
"Class1::Base". Constants are in all caps, while methods and variable =
names are lower case.

The :: operator is used to delimit namespace. Without using namespaces =
for your library, everyone who wanted to call their class "Base" would =
collide with each other. You'd get some very unpredictable behavior.

Besides preventing namespace collisions, it helps you to organize and =
describe your objects.

Scott
 
Z

Zhi-Qiang Lei

What about code like:

::Digest::MD5.hexdigest "xxx"

Why is the :: before Digest module nothing?

Class and Module names must be in their "proper case" form. So, =
"Class1::Base". Constants are in all caps, while methods and variable =
names are lower case.
=20
The :: operator is used to delimit namespace. Without using =
namespaces for your library, everyone who wanted to call their class =
"Base" would collide with each other. You'd get some very unpredictable =
behavior.
=20
Besides preventing namespace collisions, it helps you to organize and = describe your objects.
=20
Scott
=20
On Nov 6, 2010, at 8:11 PM, Guido Granobles wrote:
=20
Hi!.. I'm very new with Ruby and I wold like to know what does it = mean
the four points when I am doing inheritance. Like:
Code:
class1::base
I know that it represents a constant but how I should interpret that.
=20
--=20
Posted via http://www.ruby-forum.com/.
=20
=20
=20


Best regards,
Zhi-Qiang Lei
(e-mail address removed)
 
J

Jesús Gabriel y Galán

What about code like:

::Digest::MD5.hexdigest "xxx"

Why is the :: before Digest module nothing?

That means to refer to the top most Digest constant, to avoid
referencing the wrong one in cases like this:

module Test
module Digest
# my own Digest module
end

class TestClass
Digest # refers to my own module
::Digest # refers to the Digest module defined at the top level
end
end

Jesus.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top