Too Cool for Internet Explorer

Sunday, November 25, 2007

Ruby, double colon !


May someone call the right phrase: “Ruby period!”

I spent at least two weeks to understand this :: operator. But why ?

If you, have no C++ or C# strong background like me, certainly the :: operator will cost you more time to learn.

First of all :: appears all around Ruby programs, and this could be a little tricky…

You have to consider just one thing:

The :: is a unary operator that allows: constants, instance methods and class methods defined within a class or module, to be accessed from anywhere outside the class or module.

Remember: in Ruby, classes and methods may be considered constants too.

You need just to prefix the ::Const_name with an expression that returns the appropriate class or module object.

If no prefix expression is used, the main Object class is used by default.

So, the operator syntax is:

ClassModule::Constant

You can see samples here.

Hope it helps You not to waste time on this subject.

5 comments:

Anonymous said...

Thanks Marcos,

Actually helped a lot. I'm new to Ruby and had seen :: used in different contexts so was confused as to what it actually meant.

cheers

Anonymous said...

Thanks for the concise and clear explanation.

Charles said...

I spent 3 hours looking for your blog, thanks a lot! Helped a lot.

Dave said...

thank you very much :)

Qasim said...

Thanks. It helped a lot