What is Ruby's double-colon `::`?

后端 未结 10 1995
南笙
南笙 2020-11-22 10:09

What is this double-colon ::? E.g. Foo::Bar.

I found a definition:

The :: is a unary operator that all

10条回答
  •  花落未央
    2020-11-22 10:36

    Adding to previous answers, it is valid Ruby to use :: to access instance methods. All the following are valid:

    MyClass::new::instance_method
    MyClass::new.instance_method
    MyClass.new::instance_method
    MyClass.new.instance_method
    

    As per best practices I believe only the last one is recommended.

提交回复
热议问题