> Indeed. Class methods in Ruby are a code smell: not always wrong, but
> you should always think about why you're choosing a class-level method
> instead of instance-level.

I don't know if I'd go so far as to say they're a code smell. Without
class methods a number of very useful Ruby DSLs wouldn't exist in
their current form (RSpec, Cucumber, Rails, etc., just to name a few).
I agree that they can be abused or misused, but that's true of pretty
much any construct in any language, isn't it?

I'd probably arrange my skepticism of a class method from lowest to
highest in this way, depending on its features:

  * part of a DSL (`describe`, `Before`, `callback { ... }`, etc.)
  * repository pattern (Widget.find(...))
  * factory pattern (Widget.orange # => <orange Widget instance>)
  * method takes more than a parameter or two
  * non-factory class method with variable args
  * instance of the class method's class is in the arguments/params
list (often very questionable)

~ jf
--
John Feminella
Principal Consultant, BitsBuilder
LI: http://www.linkedin.com/in/johnxf
SO: http://stackoverflow.com/users/75170/



On Thu, Feb 17, 2011 at 10:03, Avdi Grimm <groups / inbox.avdi.org> wrote:
> On Thu, Feb 17, 2011 at 4:41 AM, Shadowfirebird
> <shadowfirebird / gmail.com> wrote:
>> I don't mean it as a put-down; I suspect the only person I'm putting down is myself.  But I don't find that class methods actually come up much in my Ruby coding; when I find myself coding one I tend to stop and think hard about whether I actually need it.
>
> Indeed. Class methods in Ruby are a code smell: not always wrong, but
> you should always think about why you're choosing a class-level method
> instead of instance-level.
>
> I think most of the class/module-level methods I write are "macros" -
> e.g. methods along the lines of Ruby's #attr_accessor.
>
> --
> Avdi Grimm
> http://avdi.org
>
>