Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Idiomatic use of double-colon (double-column, or ::) syntax for Ruby methods

Tags:

ruby

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:

  • 2007: https://www.ruby-forum.com/topic/107527
  • 2010: What is Ruby's double-colon `::`?
  • 2010: What does ::MyClass Ruby scope operator do?
  • 2010: https://www.ruby-forum.com/topic/315853
  • 2011: Double colons before class names in Ruby?
  • 2012: Ruby's double colon (::) operator usage differences
  • 2014: Ruby class naming convention with double colon
  • http://www.tutorialspoint.com/ruby/ruby_operators.htm
like image 914
Tom Baker Avatar asked Dec 20 '15 21:12

Tom Baker


People also ask

What does double colon mean in Ruby?

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.

What does the double colon :: operator do in a class?

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.

What does :: In Ruby mean?

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.


2 Answers

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:

  1. Technically speaking, there are no class methods or module methods in Ruby. Every object can have methods defined only for itself (so-called singleton methods), and since classes and modules are also objects, they can also have singleton methods. So, we will sometimes talk about "class methods" or "module functions", but in reality, those are just singleton methods.
  2. Well … actually, I lied. Sorry. Singleton methods don't exist either. They are really just normal instance methods of the singleton class of the object. But saying "instance method of the singleton class of the object" is a mouthful, so we just say "singleton method of the object", and likewise instead of "instance method of the class object's singleton class", we say just "class method". However, we only say this in the knowledge that those things actually don't really exist, and especially when talking to newbies, I prefer to use the long form instead of the short-form jargon.
  3. :: 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
    
like image 149
Jörg W Mittag Avatar answered Sep 28 '22 11:09

Jörg W Mittag


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}.

like image 30
sawa Avatar answered Sep 28 '22 12:09

sawa