Difference between . and #

后端 未结 3 925
抹茶落季
抹茶落季 2020-12-23 20:37

In Ruby what is the difference between those two (in code):

  • Class.method
  • Class#method
相关标签:
3条回答
  • 2020-12-23 21:18

    The hash format (Class#method) is not valid ruby, but is used in documentation to describe an instance method.

    Class methods are typically documented using a double-colon (Class::method).

    You will see examples of both in the ruby docs (e.g. http://www.ruby-doc.org/core-1.9.3/String.html)

    The dot format is used in code when actually calling a class method (Class.method), though I have seen some people (unfortunately) use it interchangeably with either the double-colon or hash in documentation.

    0 讨论(0)
  • 2020-12-23 21:22

    Class#method is not valid code. It is only used in documentation. method should be an instance method.

    Class.method or object.method is the actual method belonging to the object. Class is an object too. It is valid code.

    0 讨论(0)
  • 2020-12-23 21:36

    It's a naming convention.

    • use pound #method for instance methods
    • use dot .method for class methods

    See: How to name RSpec describe blocks for methods

    0 讨论(0)
提交回复
热议问题