Forum: Ruby Dumb question: in documentation, why Object#method, and not

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D29144171d94eb3e92bf411735f96d55?d=identicon&s=25 elf (Guest)
on 2005-11-30 20:21
(Received via mailing list)
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
Cee0292fffa691f1fb320d5400200e99?d=identicon&s=25 marcel (Guest)
on 2005-11-30 20:21
(Received via mailing list)
On Thu, Dec 01, 2005 at 04:17:30AM +0900, Elf M. Sternberg wrote:
> 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?

The # is a naming convention.

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.

marcel
123320fdc17940dfc8e365edb48fbff2?d=identicon&s=25 bob_showalter (Guest)
on 2005-11-30 20:25
(Received via mailing list)
Elf M. Sternberg wrote:
> 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?

Yes.
149379873fe2cb70e550c6bff8fedd0c?d=identicon&s=25 jeff (Guest)
on 2005-11-30 20:25
(Received via mailing list)
Elf M. Sternberg wrote:
> 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?

Yes.
45196398e9685000d195ec626d477f0e?d=identicon&s=25 transfire (Guest)
on 2005-11-30 20:29
(Received via mailing list)
Yes. ;-)
31ab75f7ddda241830659630746cdd3a?d=identicon&s=25 halostatue (Guest)
on 2005-11-30 20:29
(Received via mailing list)
On 11/30/05, Elf M. Sternberg <elf@drizzle.com> wrote:
> 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?

As others have said, yes. As I understand it, though, it's a
smalltalkism.

-austin
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 dblack (Guest)
on 2005-11-30 20:33
(Received via mailing list)
Hi --

On Thu, 1 Dec 2005, Marcel Molina Jr. wrote:

> The # is a naming convention.
>
> 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).


David
45196398e9685000d195ec626d477f0e?d=identicon&s=25 transfire (Guest)
on 2005-11-30 20:33
(Received via mailing list)
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.

HTH,
T.
5da4c52f43677f395aff5bde775593c2?d=identicon&s=25 Daniel Schierbeck (dasch)
on 2005-11-30 21:38
(Received via mailing list)
David A. Black wrote:
>>>
>>  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).

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)
929f667706d5d2d3513b2c50676ad08c?d=identicon&s=25 justin.w.smith (Guest)
on 2005-12-01 16:41
(Received via mailing list)
Hmm...so Class is an instance of itself?
912c61d9da47754de7039f4271334a9f?d=identicon&s=25 mental (Guest)
on 2005-12-01 16:45
(Received via mailing list)
On Fri, 2005-12-02 at 00:37 +0900, jwesley wrote:
> Hmm...so Class is an instance of itself?

Yep.

Class.class # => Class

-mental
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 dblack (Guest)
on 2005-12-01 16:49
(Received via mailing list)
Hi --

On Fri, 2 Dec 2005, jwesley wrote:

> Hmm...so Class is an instance of itself?

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.


David
78239b8634bb0a30de3b1c2cda47066b?d=identicon&s=25 gb le (gbl)
on 2009-07-28 20:17
elf wrote:
> 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.
53581739a445ad78250a676dabddf55f?d=identicon&s=25 James Coglan (Guest)
on 2009-07-28 20:23
(Received via mailing list)
2009/7/28 Gary Leydon <gary.leydon@yale.edu>

> >                 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.


http://ruby-doc.org/core/classes/Object.html#M000336
59528506e6297141161afcde91d677c9?d=identicon&s=25 Nicolai Reuschling (codeblogger)
on 2009-07-28 20:24
(Received via mailing list)
>
> 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.

I immediately found it on ruby-doc.org:
http://www.ruby-doc.org/core/classes/Object.html#M000336

Regards
Nicolai
78239b8634bb0a30de3b1c2cda47066b?d=identicon&s=25 gb le (gbl)
on 2009-07-28 21:06
Nicolai Reuschling wrote:
>>
>> 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.
>
> I immediately found it on ruby-doc.org:
> http://www.ruby-doc.org/core/classes/Object.html#M000336
>
> Regards
> Nicolai

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
http://ruby-doc.org/documentation-guidelines.html, somewhat buried but
certainly clear.

Style Guidelines

Use :: for describing class methods,
     # for describing instance methods,
     . for example code.
29194588628f0c08917c93d1b5793d3a?d=identicon&s=25 Matthias Reitinger (Guest)
on 2009-07-29 03:25
(Received via mailing list)
Gary Leydon schrieb:
>>                 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.

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 changelog[2] dates back to 1995,
suggesting that Matz himself may have introduced this convention.

-Matthias

[1]: http://www.rubycentral.com/pickaxe/preface.html
[2]:
http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/tags/v...
This topic is locked and can not be replied to.