ruby – What is the difference between calling functions with "::" and "."?

Question:

In ruby, it is possible to make a call to any method, either on an object, either using :: or using . . What is the difference between them?

Answer:

Not just any method can call like that. You use this operator to indicate that you are using a namespace , a module or class name, until you reach a member, but since you are accessing a module or class member, you can only access members that are part of the type but not the object , so static methods can be accessed, but not instance. It can also be used for fields and constants, as long as they belong to the type or module.

Example:

x = 0
module Teste
  x = 0
  ::x = 1
end

puts x #gobal, vale 1
puts Teste::x #do módulo vale 0

I put it on GitHub for future reference .

Note that if you use the operator without a name the language assumes it is the global name.

It's just the form used for name resolution with a specific qualifier. So don't confuse with . which is used for solving a member of an object.

Scroll to Top