Team,
Please be easy on me, but I was wondering why methods that belong to the
Enumerable class have a # (pound) sign. For instance:
- #all? <http://ruby-doc.org/core-2.0/Enumerable.html#method-i-all-3F>
- #any? <http://ruby-doc.org/core-2.0/Enumerable.html#method-i-any-3F>
- #chunk <http://ruby-doc.org/core-2.0/Enumerable.html#method-i-chunk>
- #collect<http://ruby-doc.org/core-2.0/Enumerable.html#method-i-collect>
Etc.
What is the meaining of the # there?
Thank you
···
--
Ruby Student
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 Mon, Jul 1, 2013 at 4:02 PM, Ruby Student <ruby.student@gmail.com> wrote:
--
Dave Aronson, the T. Rex of Codosaurus LLC,
secret-cleared freelance software developer
taking contracts in or near NoVa or remote.
See information at http://www.Codosaur.us/\.
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
···
On Jul 1, 2013, at 4:02 PM, Ruby Student <ruby.student@gmail.com> wrote:
Team,
Please be easy on me, but I was wondering why methods that belong to the Enumerable class have a # (pound) sign. For instance:
• #all?
• #any?
• #chunk
• #collect
Etc.
What is the meaining of the # there?
Thank you
--
Ruby Student
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
···
On Jul 1, 2013, at 4:18 PM, Dave Aronson <rubytalk2dave@davearonson.com> wrote:
That's why
documenting instance methods with the classname and a dot could lead
to confusion.
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
···
On Mon, Jul 1, 2013 at 5:10 PM, Robert Jackson <robert.w.jackson@me.com>wrote:
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
On Jul 1, 2013, at 4:02 PM, Ruby Student <ruby.student@gmail.com> wrote:
> Team,
>
> Please be easy on me, but I was wondering why methods that belong to the
Enumerable class have a # (pound) sign. For instance:
>
> • #all?
> • #any?
> • #chunk
> • #collect
> Etc.
>
> What is the meaining of the # there?
>
> Thank you
>
> --
> Ruby Student
--
Ruby Student