Chris McMahon wrote: > I can think of a couple of ways to do this, but they're all painful in > one way or another. Ruby being Ruby, I wonder if there's some nifty > shortcut. Given > > floats = [] > floats << 3.456 > floats << 1.53 > floats << 5.123 > > show that the least element of the array is 1.53 and the greatest > element of the array is 5.123, where the array "floats" can have an > arbitrarily large number of elements, of which all are (of course) > numbers with decimal values irb(main):001:0> floats = [] => [] irb(main):002:0> floats << 3.456 => [3.456] irb(main):003:0> floats << 1.53 => [3.456, 1.53] irb(main):004:0> floats << 5.123 => [3.456, 1.53, 5.123] irb(main):005:0> floats.min => 1.53 irb(main):006:0> floats.max => 5.123
These methods aren't simply part of the Array class. Rather they come from the Enumerable module (which Array uses). You can mix this module into any of your classes that implements two methods: each and the comparison operator (aka spaceship) that looks like this: <=> and thereby automatically get cool methods like this. You can also extend any objects that support these methods with the Enumerable method. I often do this with win32ole objects that implements the (COM) IEnumerable interface and therefore have an each method. Bret _______________________________________________ Wtr-general mailing list [email protected] http://rubyforge.org/mailman/listinfo/wtr-general
