Intridea Development Blog
Hashie: The Hash Toolkit
One of my earliest gems was Mash, a useful tool for creating mocking objects as a Hash. One of the most common problems people had with Mash was a simple one: it conflicted with another class of the same name in extlib! To address this problem as well as give the project some room to grow, Mash is now part of a new toolkit called Hashie. Hashie is available now via Gemcutter and the source, as always, is available on GitHub. To install:
gem install hashie
Hello, Hashie
Hashie is, right now, simply the former Mash code along with a new extended Hash called a Dash. A Dash is a “discrete hash” that has pre-defined properties. It can be used as a dead-simple data object when even something like DataMapper is too heavy, but a Struct is too light (Dash gives you the ability to set per-property defaults as well as initialize from an attributes Hash). For example:
class Person < Hashie::Dash property :name property :email property :occupation, :default => 'Rubyist' end p = Person.new p.name # => nil p.occupation # => 'Rubyist' p.email = 'abc@def.com' p.email # => 'abc@def.com' p['awesome'] # => NoMethodError p = Person.new(:name => "Awesome Guy") p.name # => "Awesome Guy" p.occupation # => "Rubyist"
The other advantage Hashie has over Mash is that it’s built from the ground up to avoid conflicts. Instead of adding stringify_keys methods to the Hash class, it’s instead added to a Hashie::Hash subclass. You can, however, get Hashie’s few Hash extensions in the Hash class by including the HashExtensions:
Hash.send :include, Hashie::HashExtensions
Hopefully the move will make it easier for everyone to use it in their projects without fear of running into conflicts, and hopefully you’ll also find the Dash useful. Over time the functionality of Hashie may grow to encompass additional simple and useful extensions of Hash. So install Hashie, your friendly neighborhood Hash toolkit, today!
The Intridea Blogs
Blog Archive
- January 2010 (1)
- December 2009 (1)
- November 2009 (2)
- October 2009 (1)
- September 2009 (2)
- August 2009 (2)
- July 2009 (2)
- June 2009 (2)
- May 2009 (1)
- April 2009 (3)
- March 2009 (8)
- February 2009 (9)
- January 2009 (2)
- December 2008 (1)
- November 2008 (1)
- October 2008 (2)
- July 2008 (5)
- June 2008 (8)
- April 2008 (2)
- March 2008 (1)
- February 2008 (4)
- January 2008 (4)
- December 2007 (3)
- November 2007 (3)
- October 2007 (4)
- September 2007 (2)
- August 2007 (3)
Michael Bleigh