[rspec-users] class << self considered harmful... really?

Shot (Piotr Szotkowski) shot at hot.pl
Tue Nov 25 11:11:30 EST 2008

>> On Tue, Nov 25, 2008 at 9:38 AM, Zach
>> Dennis <zach.dennis at gmail.com> wrote:

>>> The virtual class of an object (class or instance alike) gives you
>>> the ability to modify the object in ways that a simple class method
>>> usually does not allow.

In particular, when implementing a singleton this way, it’s possible to
access instance variables of the singleton (rather than having to work
on class variables).

>>> My rule of thumb is to use it when I can't easily achieve the
>>> same result using other mechanisms, but not to use it just cause'.

Ditto. I basically use it for singleton classes (Log, Config) only.

David Chelimsky:

> One thing Chris points out in his blog is that 'def self.method_name'
> is perfectly fine, and that it makes refactoring easier. My experience
> is different, because I'm often searching for methods with 'def
> method_name' - this is actually one of the reasons I have preferred
> 'class << self'.

PHP with its ‘function &returningAReference(…)’ syntax taught me to grep
for function.*name, so I now simply grep for def.*method_name in Ruby
(granted, it doesn’t work too well if you have other methods with
‘method_name’ inside their names).

-- Shot
Exchange actually reports two different INTERNALDATEs for the exact
same message when queried at different points in time. [...] Of course
no OTHER imap server I've encountered returns DIFFERENT values for
the SAME message. But it's Microsoft; what do you expect? If their
programmers were any good they'd be working at Google.   -- sup/imap.rb
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 191 bytes
Desc: not available
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20081125/244f0512/attachment.bin>

More information about the rspec-users mailing list