Ruby Colon – : & ::
here is an explanation of the colon meaning in ruby
language.
: #==> Ruby Symbol
:: #==> Ruby Namespace Resolution Operator
single colon – Ruby Symbol
a very well document, please read: ruby-symbol
definition
ruby-symbol
starts with a colon :
, it either can be a string or a sentence with white spaces (must be quoted)
:my_symbol
OR
:"my symbol string"
symbol is immutable
ruby-symbol
holds the value of its defining, it cannot be changed or redefined.
for example;
:my_symbol # has the value of "my_symbol"
:my_symbol = "your_symbol" # wrong, symbol is immutable
method
it has two main parts, string-representation
(its definition) & integer-representation
(its reference)
puts :my_symbol #==> my_symbol, --- most time it refers string representation
puts :my_symbol.to_s #==> my_symbol, --- string representation
puts :my_symbol.to_i #==> a reference integer number, --- integer representation
#==> deprecated since version 1.9, use `.object_id` instead
puts :my_symbol.class #==> Symbol
ruby-symbol
is not the String
, even with string-representation it can use all String
methods.
rather, it is a thing (like container) that has both string-representation and integer-representation
for example:
"foo".equal? "foo" #==> false
"foo" == "foo" #==> true
:foo.equal? :foo #==> true
:foo == :foo #==> true
usage
- a constant, don't need to change its value at runtime
this will be extremely helpful when the same constant is accessed in innumerous times, if used with symbol
, it will have a great boost of performance.
double colon – Ruby Namespace Resolution Operator
namespace
plays an very important role in many computer languages.
for more, please read: wikipedia-namespace
it helps to access constant, module or class defined inside other classes or modules.
its definition:
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.
for ruby-namespace-scope
, all their beginning top level is Object
which means if a namespace is defined like ::namespace
, then it is in a global-scope
, same like Object::namespace
in nested module, avoid using ::
, because for namespace, it only means following the way in sequentially opening the module, which apparently does not equal to defining a module.
example
module A
module B
Info = "inside B, constant Info"
end
end
module A
module B
module C
class D
def m
puts Info
end
end
end
end
end
A::B::C::D.new.m #==> works well
however,
module A
module B
Info = "inside module B"
end
end
module A::B::C
class D
def m
puts Info
end
end
end
A::B::C::D.new.m #==> uninitialized constant A::B::C::D::Info (NameError)