Why are methods in Ruby documentation preceded by a hash sign?

前端 未结 8 781
小蘑菇
小蘑菇 2020-11-30 21:05

When I see any Ruby method printed in text, it usually appears as:

Class#method

or

#method

Now, I would u

8条回答
  •  生来不讨喜
    2020-11-30 21:58

    I just realized that none of the other answers touch the most trivial aspect of the question: why the # sign?

    I have two theories:

    1. It might come from Smalltalk, where symbols are written #sym (instead of :sym) as they are in Ruby. So, if you want to refer to a Method object (as opposed to calling a method), then you would call something like Array >> #new. (The >> is itself a method that returns the method passed to it. So, in Ruby that would be Array.method :new.) In Smalltalk documentation, methods are generally referred to as Class>>method, but in Ruby Class:method would have made more sense, except that it is easily confused with Class::method. Therefore, Class#method was chosen.
    2. My other theory is that it simply was chosen because # is the comment character in Ruby.

    A definitive answer can only be given by whoever invented that convention. If it was invented for the Programming Ruby book, that would be either Dave Thomas or Andy Hunt, but I kind of doubt that. The book came out in 2001, Ruby started in 1993, how were they referring to methods before then?

提交回复
热议问题