CORE API |
MORE API |
The method #collect, or its alias #map if your prefer, is one of the most useful methods in Rubyland. You see it all the time. Unfortunately it isn't applicable to a number of common variants of iteration. One of these is Integer#times. So Facets provided just such a method.
bottles = 99.times_collect { |i| "Bottle of Beer No. #{i}" }
Take one down, pass it around!
Facets has been blessed with an incredibly comprehensive Units system written by Peter Vanbroekhoven. The system handles all the standard SI units, plus provides currency exchange figures via web service. It's an amazingly comprehensive system. Just have a look at some of these examples!
1.bit/s + 8.bytes/s #=> 65.0 bit/s (1.bit/s).to(byte/s) #=> 0.125 byte/s 1.mile.to(feet).to_s #=> 5280.0 feet 1.acre.to(yd**2) #=> 4840.0 yd**2 1.acre.to(sq_yd) #=> 4840.0 sq_yd 1.gallon.to(self.L) #=> 3.785411784 L 1.lb.to(kg) #=> 0.45359237 kg 1.m.s.to(m.s) #=> 1 m s 10.m/10.s #=> 1 m/s 10.meter/10.second #=> 1 meter/second 1.sq_mi.to(km**2) #=> 2.5899881103 km**2 1.mile.to(km) #=> 1.609344 km 1.usd.to(twd) #=> 33.275000380 twd 1.cwt(:uk).to(lb(:uk)) #=> 112.0 lb 1.cwt(:us).to(lb(:us)) #=> 100.0 lb with_unit_converter(:uk) { 1.cwt.to(lb) #=> 112.0 lb } with_unit_converter(:us) { 1.cwt.to(lb) #=> 100.0 lb }
At the moment the initial load-up takes a second or two to fetch the currency exchnage rates, so don't be surprised by that. An upcoming version will take care of this. Seems Peter has some other ideas for additional math-oriented libs too. I joked with him, that if he kept at it he'd have a Ruby Mathematica on his hands. He returned, "That's the idea."
[NOTE: This module used to be called EnumerableArgs.]
Has it ever occurred to you that you can't define an #each method which takes parameters and have it work with the Enumerable mixin? It's true. Try it and you'll get an ArgumentError. Enumerable's methods just weren't designed to pass through any parameters to #each. Well, if you've never thought about it before, I suppose it's never been a bother. But if you're one of the unlucky few who has actually tried to do so, probably not thinking much of it, you know just how frustrating it is. You are reduced to writing your own #find, #map, #select and the many other normally eager-to-mix methods of Enumerable.
No Worries. EnumerablePass is here. Simply include it into your class and it will take care of the rest. It's quite nearly 100% compatible to Enumerable in every way. So you shoudn't miss a beat.
Here's a simply example of how EnumberablePass can be useful.
class << ObjectSpace include EnumerablePass alias :each, :each_object end
With the above it becomes possible to use all the familar Enumerable methods when looking through Ruby's object space!