I am relatively new to Ruby and find it confusing that the following pairs of examples work equally well:
File.included_modules
File::included_modules
File.stat('mbox') # Returns a '#<File::Stat..>' object
File::stat('mbox')
File.new("foo.txt", "w")
File::new("foo.txt", "w")
"asdf".size # An instance method
"asdf"::size
2 + 3
2::send(:+, 3) # An extreme example
File::new
, in particular, is something I quite frequently encounter.
My question: Would it be non-idiomatic for me to avoid ever using the :: operator for qualifying the names of anything but classes, modules, and constants and, instead, consistently to use only dot syntax for all methods (class methods, module methods, and instance methods)?
To be clear: are there any situations in which I would want to refer to a method (whether in code or in documentation) using anything other than dot syntax?
Sources consulted:
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.
The Scope Resolution Operator (also called Paamayim Nekudotayim) or in simpler terms, the double colon, is a token that allows access to static, constant, and overridden properties or methods of a class.
The use of :: on the class name means that it is an absolute, top-level class; it will use the top-level class even if there is also a TwelveDaysSong class defined in whatever the current module is.
Would it be non-idiomatic for me to avoid ever using the
::
operator for qualifying the names of anything but classes, modules, and constants and, instead, consistently to use only dot syntax for all methods (class methods, module methods, and instance methods)?
No, it would in fact be idiomatic. Nobody ever uses ::
to call methods, ever. Period.
The only time ::
is used in conjunction with methods is when talking about methods. In that case, Foo#bar
is used to talk about instance methods (i.e. a method bar
that can be called on instances of Foo
like foo.bar
) and Foo::bar
is used to talk about singleton methods (i.e. a method bar
that can be called on Foo
itself like Foo.bar
). However, we only use ::
for talking about methods, never for calling them.
Some notes:
::
is used to dereference constants inside modules. The fact that those constants often point to modules or classes is not significant. Here is an example of resolving a constant which doesn't point to a module or class inside a module that isn't referenced by constant:
module Foo; BAR = 42 end
foo = Foo
foo::BAR # => 42
# ^ ^------------ not a module
# |
# +---------------- not a constant
Would it be non-idiomatic for me to avoid ever using the :: operator for qualifying the names of anything but classes, modules, and constants and, instead, consistently to use only dot syntax for all methods (class methods, module methods, and instance methods)?
Regarding the ::
for method calls, I don't think there is any situation where you cannot substitute it with .
, or where ::
is preferred.
[A]re there any situations in which I would want to refer to a method (whether in code or in documentation) using anything other than dot syntax?
When the receiver is self
, it is generally preferred to omit the self
and .
unless the method is either of foo=
form, or is either []=
or class
.
When there is need to distinguish from a local variable with the same name as the method, explicit receiver and .
can also be used, but ()
can be used instead.
When you need to call a private method with a receiver other than self
, you can use receiver.send(:method_name)
or receiver.instance_eval{method_name}
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With