Notice that “Object” in “Object#method” is capitalized. We’re talking
about a class here not an instance. So if you were to say Object.method
you’d be refering to an instance method of the class itself --generally
called a class method, as opposed to an instance method defined by
the class. It’s tricky because its relative --a class is itself an
instance of Class.
Interesting – I never picked up on ri’s use of :: in preference to
the dot. I personally use Class.method for class methods (indeed for
any singleton method).
I believe that to be the convention, too.
Klass.method <- Class method
Klass#method <- Instance method
Klass::FOO <- Class constant
Klass::AnotherKlass <- Class constant (class or module)
Class#method means method is an instance method.
Class::method means method is a class method.
For example, do
ri IO.read
There is IO::read and IO#read.
Interesting – I never picked up on ri’s use of :: in preference to
the dot. I personally use Class.method for class methods (indeed for
any singleton method).
Yes. And Object is a Class, and Class is an Object Ruby
object-space chases its own tail a bit at the top of the hierarchy,
for the sake of bootstrapping itself into existence.
Where is this documented? I can’t find it in any book…and searching
google on Ruby object#method doesn’t lead to an explanation of this. You
get reams of hits using object#method syntax but not where it comes
from.
Thanks for taking the time, I suspect I didn’t make my question
clear,there is nothing that I can see on the link Object.html#M000336
that specifies what object#method means vs object::method but your link
lead me to search on the site and I found it here Ruby Documentation Submission Guidelines, somewhat buried but
certainly clear.
Style Guidelines
Use :: for describing class methods,
# for describing instance methods,
. for example code.
Where is this documented? I can’t find it in any book…and searching
google on Ruby object#method doesn’t lead to an explanation of this. You
get reams of hits using object#method syntax but not where it comes
from.
The 1st edition of "Programming Ruby"1 (a.k.a. the “Pickaxe”) mentions
this notation:
Within the text, Fred#doIt is a reference to an instance method (doIt) of
class Fred, while Fred.new [In some other Ruby documentation, you may
see class methods written as Fred::new. This is perfectly valid Ruby
syntax; we just happen to feel that Fred.new is less distracting to
read.] is a class method, and Fred::EOF is a class constant.
The first appearance in the Ruby changelog2 dates back to 1995,
suggesting that Matz himself may have introduced this convention.
I keep seeing this syntax in documentation: Object#method, but in actual
use sending a message to an object is usually
object = Object.new
object.method
Is this just to emphasize that method() operates on an instance of
Object and not the class Object?
Elf
Where is this documented? I can’t find it in any book…and searching
google on Ruby object#method doesn’t lead to an explanation of this. You
get reams of hits using object#method syntax but not where it comes
from.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.