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.
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.
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.