Ruby Syntax - One::Two

T

toulax

I'm a newcomer to ruby and there's this little something I don't
really understand:

class One::Two
....
end

Class One::Three
....
end

On this example, what does "::" mean? If understand correctly it's
just sort of a naming convention to imply that both the Two and Three
classes belong to the same "group", as in Net::HTTP and Net::FTP.
So, in essence Two and Three are completely unrelated, and I'd not
even need to have a class named One for it to work, is that right?

If not, please tell me, what exactly is the purpose of "::" on class
names?

Thanks
 
S

Stefano Crocco

Alle venerd=EC 16 febbraio 2007, (e-mail address removed) ha scritto:
I'm a newcomer to ruby and there's this little something I don't
really understand:

class One::Two
...
end

Class One::Three
...
end

On this example, what does "::" mean? If understand correctly it's
just sort of a naming convention to imply that both the Two and Three
classes belong to the same "group", as in Net::HTTP and Net::FTP.
So, in essence Two and Three are completely unrelated, and I'd not
even need to have a class named One for it to work, is that right?

If not, please tell me, what exactly is the purpose of "::" on class
names?

Thanks

:: is the scope operator. It is used to access constants defined in a class=
or=20
module. Since classes are constants, when you write class One::Two, you're=
=20
defining the class Two inside One (where One can be a class or a module). I=
f=20
you didn't define One, the code class One::Two will give you a NameError,=20
telling you that the constant One is not initialized:
NameError: uninitialized constant One
from (irb):1
Another example of the use of :: is=20

puts Math::pI, which prints the value of the PI constant
3.14159265358979

Since PI belongs to the Math module, I couldn't have written puts PI. In fa=
ct,=20
this raises an exception:
NameError: uninitialized constant PI
from (irb):5

I hope this helps.

Stefano
 
T

toulax

Alle venerdì 16 febbraio 2007, (e-mail address removed) ha scritto:









:: is the scope operator. It is used to access constants defined in a class or
module. Since classes are constants, when you write class One::Two, you're
defining the class Two inside One (where One can be a class or a module).If
you didn't define One, the code class One::Two will give you a NameError,
telling you that the constant One is not initialized:


NameError: uninitialized constant One
from (irb):1



Another example of the use of :: is

puts Math::pI, which prints the value of the PI constant


3.14159265358979

Since PI belongs to the Math module, I couldn't have written puts PI. In fact,
this raises an exception:


NameError: uninitialized constant PI
from (irb):5

I hope this helps.

Stefano

I understand now, I didn't realize "class One::Two" actually defined
the constant Two inside One.
Thank you!
 

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,234
Messages
2,571,178
Members
47,809
Latest member
Adisty

Latest Threads

Top