Why the use of a pound (#) sign with Enumerable

Team,

Please be easy on me, but I was wondering why methods that belong to the
Enumerable class have a # (pound) sign. For instance:

#collecthttp://ruby-doc.org/core-2.0/Enumerable.html#method-i-collect

Etc.

What is the meaining of the # there?

Thank you

On Mon, Jul 1, 2013 at 4:02 PM, Ruby S. [email protected]
wrote:

Please be easy on me, but I was wondering why methods that belong to the
Enumerable class have a # (pound) sign. For instance:

#all?

What is the meaining of the # there?

It’s not just Enumerable, it’s all Ruby classes. The # denotes an
instance method (in docs, not code), while :: is used for class/module
methods (in both docs and code).

So for instance if we have class Foo, with instance-method bar and
class-method quux, bar would be documented as Foo#bar and used in
code
as whatever_you_call_your_variable.bar, which is why we can’t
document it that way (we don’t know what the variable name will be).

Meanwhile, quux would generally be documented as Foo::quux… and
could be used as either Foo::quux or Foo.quux. That’s why
documenting instance methods with the classname and a dot could lead
to confusion.

-Dave

On Jul 1, 2013, at 4:18 PM, Dave A. [email protected]
wrote:

That’s why
documenting instance methods with the classname and a dot could lead
to confusion.

Point taken. I have been using the dot for referencing class methods,
but can definitely see where that might be confusing.

Robert Jackson

– twitter: rwjblue
– github: rjackson

These sorts of things are somewhat difficult to google for (I tried to
find a nice article to reference, but couldn’t find one quickly.), so
hopefully this helps…

The methods themselves do not have a ‘#’ in front of them.

The ‘#’ is usually used to indicate an instance method and often ‘.’ is
used to indicate a class/module method.

So given the following class:

class FooBar
def self.say_hello
puts ‘hello from a class method’
end

def say_hello
puts ‘hello from an instance method’
end
end

FooBar#say_hello references the instance method.
FooBar.say_hello references the class method.

Robert Jackson

– twitter: rwjblue
– github: rjackson

These are excellent replies. I tried google before I tried the forum
and
it did not come up with anything useful.
Hopefully people will be able to find this discussion on google later
on.

Thank you