Hashie: The Hash Toolkit

by Michael Bleigh on November 12, 2009 • filed under announcements gems open-source hashie mashcomment

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!

Comment | 
blog comments powered by Disqus

Words we've written view all blog posts »

Featured Article

Intridea Insider: Ping Yu

by Renae Bair on July 30, 2010

Ping came on board with Intridea during our infancy. He was employee #4, and has been part of the vision and culture of our company from the beginning. He has helped to grow Intridea from a group of four guys working at their dining room tables, to a company of 40+ Ruby and Rails developers and designers that span continents and multiple time zones. His work as Director of Asian Operations aims to bridge our Asian team of Rubyists with our centralized team in the states. "I believe in helping to extend Intridea into China. There is so much talent in China, it feels like a natural thing to do. Intridea has a culture of quality, and I want to bring that to these developers. They love Ruby, and they have so much energy."

Continue reading »

Recent Blog Posts

Intridea Insider: Ping Yu

by Renae Bair on July 30, 2010

Talkin' About Upgrading to Rails 3

by Michael Bleigh on July 26, 2010

A To-Go Plate of Ruby Midwest

by Michael Bleigh on July 19, 2010