Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

Class#method 

or

#method 

Now, I would use:

Class.method 

Why are all Ruby methods preceded by a pound sign? Is there any reason for it?

like image 594
Ed S. Avatar asked Apr 09 '09 22:04

Ed S.


People also ask

What does :: mean in Ruby documentation?

Use :: for describing class methods, # for describing instance methods, and use . for example code.

What is pound sign in Ruby?

The "#{}" syntax is called 'interpolation' and the pound was picked most likely because interpolation is similar in a sense to commenting, because you are changing the context of your code (in this case to another context for execution)


2 Answers

Note that the convention is:

Class#method 

rather than

object#method 

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

From the RDoc documentation:

Use :: for describing class methods, # for describing instance methods, and use . for example code.

like image 59
Simon Nickerson Avatar answered Nov 10 '22 01:11

Simon Nickerson


The # notation is used to refer to the canonical instance method, like String#upcase. The . notation is used to refer to the method of a particular instance, like mystring.upcase. The distinction is made to not imply that a class method 'upcase' exists.

like image 30
dstnbrkr Avatar answered Nov 10 '22 00:11

dstnbrkr