Notes on the Facets Gem
Author: Duane Johnson
25
Apr
The Facets gem is fairly popular, and for good reason. I was poking through the documentation on the plane home from Chicago (I have a contract position there for the next month) and found a couple of neat things to share:
Facets library
- Autoarray class—an Array that autovivifies any number of dimensions. For example:
a = Autoarray.new
a[3][4][5] = 1would create a 3-dimensional array with values initialized to ‘nil’ and the appropriate cell set to 1.
- Dictionary class—a Hash that retains order. The order, by default, is whatever order its contents were given in; however, it can also enforce any arbitrary ordering that you provide.
- Enumerable extensions—a (very nice) module that extends the Enumerable mixin. It’s huge! There are so many idioms captured in this that I can’t explain them all. But if you like Ruby for it’s concise syntax and easy chaining of things like ‘each’, ‘map’ and ’select’, you’ll like what you see in here.
- File.rewrite—open, modify, write back to disk, all in a single ruby block.
- FileUtils.head and FileUtils.tail—handy functions that let you get at the first few or last few lines of a file.
There’s plenty more to look at in the Facets gem… these are just a few that caught my attention. Install the gem and then use “gem server” to check out the documentation 
One Response for "Notes on the Facets Gem"
A word of caution: facets has functions that overlap Rails’ built-in functions, iirc some have different parameters or return values.
Make sure to require only the parts of the library you really need and check that you don’t overwrite existing Rails function by mistake.
Leave a reply