The Wayback Machine - https://web.archive.org/web/20051215180825/http://facets.rubyforge.org:80/


Methods

There are over 400 methods in the Atomic Methods collection!


Classes
and Modules




Copyright (c) 2005 Thomas Sawyer




Ruby Facets is the single largest collection of core extension methods and standard module additions available for the Ruby Programming language.

Ruby Facets is an open-source collection of methods, modules, classes and microframeworks for the Ruby programming language. The collection offers a vast set of high-quality general-use "parts" to help you build your applications.

Of particular uniquiness to Ruby Facets is the atomicity of its core extensions library. Each core method is requirable independent of any other, allowing for very fine-grain control of effects. For this reason, this portion of Facets is reffered to as Ruby's Atomic Library.

The gneral philospohy of Facets can be summed up in a simple mantra, "ALL BASE COMMON". The notion is that by sharing a large common foundation, that foundation can better serve us. There are a number of advantages to this approach.

        * Name Consistency
        * Better Code-reuse
        * Collaborative Improvements
        * One-stop Shop and Installation
      
Spotlight - Units

Facets has been blessed with an incredably comprehensive Units system written by Peter Vanbroekhoven. The system handles all the standard SI units, plus provides currency exchange figures via web serive. It's an amzingly comprehenvise 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.589988110336 km**2
        1.mile.to(km)           #=> 1.609344 km
        1.usd.to(twd)           #=> 33.2750003803837 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 take a second or two to grab the currenty info, 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."

Spotlight - String#margin

Perhaps you've come across that occasional need to assign a string that was more pictoral in character than textual. [ Pun intended :-) ] You've tried Ruby's various built string consturctors, including those goovy HERE documents, you even tried letting the HERE text butt up against column zero. Ick! In the end you either had line after line of str << "....", counted "\n"s or said, "Heck wit it!". And loaded from a file. Well, no longer! Nano contains a great little String method called #margin. It works like this:

      x = %Q{
            | This
            |     is
            |       margin controlled!
            }.margin
    

Of course you can use HERE documents if you prefer.

      x = <<-HERE.margin
            | This
            |     is
            |       margin controlled!
            HERE
    

If you dislike the particular deliminator '|', or actually need to start lines with that character, then just pick another.

      x = %Q{ This
            -     is
            -       margin controlled too!
            }.margin
    

The trick? It uses the first non-whitespace character on the 2nd line(tm). Clever, eh?

But wait. There's more! #margin also takes a parameter, with which you can specify the number of extra spaces to insert in place of the deliminator. So even if you need to push some ascii art text over 30 characters, there's no need for all that whitespace. Just specify the number.

Hope you've enjoyed this Spotlight. Now you too can produce nice and readable margin controlled strings in your code too with ease!

Spotlight - EnumerableArgs

Has it ever occured 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 muuh 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.

Fret no longer! EnumerableArgs 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 EnumberableArgs can be useful.

        class ObjectSpace
          include EnumerableArgs
          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!

Legaliese

Ruby Facets, Copyright (c) 2005 Thomas Sawyer

Ruby Facets is provided under the Ruby License.

THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.