News for hackers. Stuff that matters.
Re:My Objection to Array#sum by wvenable on Thursday April 09, 2009 @01:29PM (#554741)
I'm not a Ruby programmer so I have a simple question: Is it common to request whether an object implements a particular method before you make any sort of call? That seems considerably more fussy than strong typing of interfaces or even the type-hinting of interfaces that PHP has.

I can't help thinking a cleaner solution is the more traditional way: Create a subclass called NumberArray that can only hold numbers and has specific methods for operating on them. But I guess that might not be the "Ruby-way".

Re:My Objection to Array#sum by raganwald on Thursday April 09, 2009 @01:31PM (#554747)
> Create a subclass called NumberArray that can only hold numbers and has specific methods for operating on them.

I can't see anything wrong with this for some cases. Another way to get there would be to define a NumericCollection module that can be used to extend any Array.

Re:My Objection to Array#sum by tjstankus on Thursday April 09, 2009 @05:27PM (#555199)
I would tend toward that idea: a module that can be included. Injecting a sum method into the object's ghost class also maintains scope of responsibility, but feels a bit more magic. I'd opt for less magic. At first, anyway. :)
Re:My Objection to Array#sum by omouse on Thursday April 09, 2009 @07:05PM (#555344)
How about create a method called detectSum that accepts a block as an argument. The block will return the specific field to use when summing.

This lets you skip subclassing.

Re:My Objection to Array#sum by stonemetal on Thursday April 09, 2009 @01:35PM (#554752)
It isn't typical. The author was using it as a way to show what a class claims to support.