From: Gennady Date: 2004-08-11T05:29:29+09:00 Subject: Re: Singleton class methods Robert Klemme wrote: > "Gennady" schrieb im Newsbeitrag > news:4118FEE9.10002@tonesoft.com... > >>Kyle Putnam wrote: >> >>>Hello, >>> >>>It seems these two syntaxes accomplish the same thing, or are there >>>differences? >>> >>> class Foo >>> class << self >>> def bar >>> # ... >>> end >>> end >>> end >>> >>>compared to... >>> >>> class Foo >>> def Foo.bar >>> # ... >>> end >>> end >>> >>>Cheers, >>> Kyle >>> >>> >> >>Exactly the same from the functionality point of view (it differs in >>implementation, though). > > > Where exactly do you see this difference? Please see my reply to David Black's message, I really meant to place it here, sorry. > > >>I personally prefer the first form, as when it >>comes to renaming a class, you can do it in one place. > > > This is also possible with > > class Foo > def self.bar > # ... > end > end > > I base my decision which idiom to use on the number of methods. If it's > just one or two, I usually take the def self.foo approach. With "class << self" form it is also convenient to fold all class methods at once in vim, I like that :-) > > >>Also with this >>form you can do other nice things such as defining class attribute >>accessors with attr_reader, attr_writer or attr_accessor. > > > Yep! > > Kind regards > > robert > >