What is the difference between the function call with ":" and "."?

Asked

Viewed 58 times

4

In ruby it is possible to make a call from some method, either from some object, either using :: how much using .. What is the difference between them?

1 answer

6


It is not any method you can call that. You use this operator to indicate that you are using a namespace, a module or class name, until it reaches a member, but since it is accessing a module member or class, it can only access members that are part of the type, but not of the object, so static methods can be accessed, but not of instance. You can also use for fields and constants, provided 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 in the Github for future reference.

Note that if you use the operator without a name the language considers it to be the global name.

It’s just the form used to resolve names with a specific qualifier. So don’t confuse it with the . which is used for resolution of a member of an object.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.