May someone call the right phrase: “Ruby period!”
I spent at least two weeks to understand this :: operator. But why ?
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:
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
Thanks for the concise and clear explanation.
I spent 3 hours looking for your blog, thanks a lot! Helped a lot.
thank you very much :)
Thanks. It helped a lot
Post a Comment