Reputation: 4970
Both .
and ::
can be used to call class methods, but, in my experience, .
is by far the most commonly used of the two. So I am accustomed to using that form in documentation and RSpec describe/context/expect strings.
However, the Ruby API documentation uses ::
(e.g. at https://rubyapi.org/3.1/o/string). Is that intended to mean that that form is preferred for the cases I described?
Note: This is not a duplicate of Is there a difference between :: and . when calling class methods in Ruby?. That question refers to the use of the two alternate notations in Ruby source code, whereas this question refers to documentation and other textual descriptions (e.g. in rspec strings). There may be reasons to make different choices in code vs. documentation, for example, that using .
in code more clearly indicates a message call vs. a constant access, whereas in documentation ::
might be preferred to more dramatically distinguish class methods from instance methods.
Upvotes: 0
Views: 655
Reputation: 327
As far as I know RuboCop always suggets to use .
when calling class method, as explained here.
Looking at documentation you have linked regarding the class String
, they seem to use a single .
when calling class methods, as seen here.
Actually I was not able to find a code snippet in that doc page that uses the ::
notation for calling a class method.
If you are referring to the menu on the left, which uses ::
to indicate class methods and #
for instance methods, it's a Ruby documentation convention and has no real meaning in actual code.
Maybe this could be an helpful resource.
Upvotes: 1