Is there a difference between :: and . when calling class methods in Ruby?

前端 未结 2 1792
甜味超标
甜味超标 2020-12-18 18:08

Simple question, but one that I\'ve been curious about...is there a functional difference between the following two commands?

String::class
String.class
         


        
2条回答
  •  时光说笑
    2020-12-18 18:55

    The . operator basically says "send this message to the object". In your example it is calling that particular member. The :: operator "drills down" to the scope defined to the left of the operator, and then calls the member defined on the right side of operator.

    When you use :: you have to be referencing members that are defined. When using . you are simply sending a message to the object. Because that message could be anything, auto-completion does not work for . while it does for ::.

提交回复
热议问题