Why do methods in Ruby documentation precede a hash sign?

This is what annoyed me for a while. When I see any Ruby method printed in the text, it usually looks like:

Class#method 

or

 #method 

Now I would use:

 Class.method 

Why are all Ruby methods preceded by a pound sign? Is there a reason for this? Just curious.

+50
ruby documentation
Apr 09 '09 at 22:31
source share
7 answers

From rdoc docs :

Class names, source files, and any method names that contain an underscore or that is preceded by a hash character automatically hyperlinks from the comment text to their description.

(Emphasis added.)

+13
Apr 09 '09 at 22:41
source share

Please note that the agreement:

 Class#method 

but not

 object#method 

In code, you will have object.method if object was an instance of class . The convention # not used in the code.

From the RDoc documentation :

Use :: to describe class methods, # to describe instance methods and usage. for example code.

+94
Apr 09 '09 at 22:40
source share

The notation # is used to denote the canonical instance method, for example String#upcase .. the notation is used to denote the method of a specific instance, for example mystring.upcase . The difference is that it does not follow that there is a method of class "uppercase".

+26
Apr 11 '09 at 15:04
source share

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

I have two theories:

  • This can come from Smalltalk, where characters 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), you would call something like Array >> #new. (The >> itself is a method that returns the method passed to it. So, in Ruby it will be Array.method :new .) In the Smalltalk documentation, methods are usually called Class>>method , but in Ruby Class:method would be more reasonable, for except that it is easily confused with Class::method . Therefore, the selected Class#method .
  • My other theory is that it was simply chosen because # is a comment symbol in Ruby.

The final answer can only be given by the one who came up with this agreement. If it were invented for the book "Ruby Programming", it would be either Dave Thomas or Andy Hunt, but I kind of doubt it. The book was published in 2001, Ruby began in 1993, how did they refer to methods before?

+17
Aug 22 '09 at 21:36
source share

All of the above answers are correct. The only thing I would like to add is that in the style of the documentation that you talked about, you will transfer

Class.method

It will be easy to confuse with class methods. Since you can call class methods in ruby โ€‹โ€‹using the syntax above:

 class Foo def self.say_hi puts "hi" end end Foo.say_hi # => prints "hi" 
+7
Apr 11 '09 at 15:03
source share

This was mentioned in the JS version of this question , but it seems that this nomenclature came from JavaDoc , where the hash tag is translated directly into the link on the page, for example href="Component.html#getComponentAt(int, int)"

+3
Aug 29 '13 at 21:46
source share

The answer of heff (which I cannot comment due to lack of reputation) that Ruby follows the example of JavaDoc is the best guess in my opinion. JavaDoc developers needed or wanted to distinguish between package classifiers (which they used for the point) for class classifiers (for which they used the hash for). The syntax of the JavaDoc @see and @link syntax is as follows:

 @see package.class#member [optional label] {@link package.class#member [optional label]} 

See the JavaDoc documentation package.class for the @see tag variant and the JavaDoc @link tag documentation that it already pointed to.

In JavaDoc, the package name can often be omitted, so that only the part of the class member # will remain, which looks just as strange as in Ruby, because Java code uses the Class.member syntax as Ruby does.

It would be interesting to know why JavaDoc developers need a different syntax, while the Java compiler does a great job with dots for both purposes.

+1
Feb 19 '14 at 9:24
source share



All Articles