From: dblack@... Date: 2002-12-02T00:16:42+09:00 Subject: Re: class << self (was Re: [FAQ] What do some of Ruby's symbols mean?) Hi -- On Sun, 1 Dec 2002, Gavin Sinclair wrote: > From: "Shannon Fang" > > > > Hi, > > On Sun, 1 Dec 2002 23:35:30 +0900 > > ts wrote: > > > > > > > If instance methods are stored in the class, where do you think that the > > > class methods are stored ? > > > > I'm sorry I can't understand the purpose of your question. Is it related > > to understanding class << object? The level I'm interested in is for > > application developers, why they need to use <<, does it make their life > > easier, or...? I did not consider to make the language developer's life > > easier.. (sorry for being selfish :-p). > > > > Shannon > > > You know what a class method is, right? > > class Example > def Example.foo > 19 > end > end > > Example.foo # -> 19 > > > The same is achieved with > > class Example > class << self > def example > 19 > end > end > end > > I don't know why and I don't like it. I'm happy enough with > singleton objects (but what's wrong with "def obj.meth"?): Nothing, but it can get tedious: class A def self.thing @thing end def self.thing=(x) @thing = x end end as opposed to: class A class << self attr_accessor :thing end end (You could of course just add the attribute to the object's real class: class Class attr_accessor :thing end but then every Class object (String, Array, etc.) would have this attribute.) The class << obj idiom is based on two fundamental things about Ruby: 1. you can open/reopen any class: class String ... class MyClass ... 2. every object (almost) has a singleton class So class << obj is just a way of accomplishing #1 with regard to #2. It thus provides a way to treat singleton classes very much like generic (if that's the word) classes, which makes for what I think is a nice consistency and seamlessness. But there's no imperative that you use this idiom, if you don't like it and can write around it. David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav