Home > Software Development > Ruby Hashie Library

Ruby Hashie Library

January 28th, 2015 Leave a comment Go to comments

Ruby is a very interesting programming language. One powerful feature is its meta programming capability, which allows you to change the programming constructs at run-time. For example, you can change the definition of an existing class from standard library, which could be useful and dangerous at the same time. That is of course a whole other topic.

While working with one of my clients, I started to program Ruby. Although I learned a bit Ruby almost 10 years ago and never used it afterwards, I didn’t find it difficult to use it for a real project. Using Ruby is actually fun.

Time to learn how to "Google" and manage your VMware and clouds in a fast and secure


One library I found very useful is the Hashie library. With normal hash object in Ruby, accessing the value of a key is very easy but not as intuitive as accessing a property of an object. With the Hashie library, it’s possible to access a hash object just like accessing an object.

There are a few classes in the Hashie library. Here are a few samples to show how they work.


A Mash object is a hash that can be access using . convention. The following simple sample illustrates how to convert a hash to a Mash object and access a key like a property. If you don’t have an existing hash, you can just create on the fly like the freemash.

require 'hashie'
bookhash = {
  :title => "VMware VI and vSphere SDK",
  :author => "Steve Jin"
bookmash = Hashie::Mash.new(bookhash)
puts bookhash[:title]
puts bookmash.title
bookmash.isbn = "0137153635"
puts bookmash.isbn
freemash = Hashie::Mash.new
freemash.cloud = "AWS"
puts freemash.cloud

After reviewing the code, you may agree with me that using the Mash object makes the code more intuitive and cleaner.


With hash and above Mash object, you can add any key value pairs without any limitation. But what if you have a pre-defined set of keys and only these keys can be accessed? This allows a hash to act more like a real object with properties.

This is where the Dash comes to help. As show in the following sample, the Book object is subclassed from Dash class with a few properties defined. The Book class can then used as a normal class. If an un-defined property, an exception will be thrown. Notice that it’s not property not found, but NoMethodError which suggests that the Hashie library uses meta programming.

require 'hashie'
class Book < Hashie::Dash
  property :title
  property :author
  property :isbn
  property :year
book = Book.new
book.title = "VMware VI and vSphere SDK"
book.author = "Steve Jin"
book.isbn = "0137153635"
book.year = 2009
book.url = "http://amzn.to/97ZyAg" # => C:\Users\sjin\NetBeansProjects\demo\lib\hashie_demo.rb:16:in `<main>': undefined method `url=' for #<Book:0x007fdc01804960> (NoMethodError)
print "book as Dash object:\n" + book.to_s # => #<Book author="Steve Jin" isbn="0137153635" title="VMware VI and vSphere SDK" year=2009>
print "\nbook as Hash object:\n"  + book.to_hash.to_s # => {:title=>"VMware VI and vSphere SDK", :author=>"Steve Jin", :isbn=>"0137153635", :year=>2009}

From Mash to Dash

Although both Mash and Dash are inherited from same class, it’s not that easy to convert them.

Why is it needed? Think about this use case. You have data like Book persisted to XML or JSON, then you want to get them back. You can of course use Mash, which is easy but does not have enforcement on the keys. In other words, you want to have a validated Book object, at least, you know what keys are there.

As discussed above, the conversion could fail because of extra therefore un-recognized keys. If all the keys are conforming, it should succeed.

The following code shows how to do it. The real trick is to pass in the symbolize_keys: true while calling the to_hash() method.

require 'hashie'
class Book < Hashie::Dash
  property :title
  property :author
  property :isbn
  property :year
bookmash = Hashie::Mash.new
bookmash.title = "VMware VI and vSphere SDK - 1st Edition"
book = Book.new(bookmash.to_hash(symbolize_keys: true))
puts book.title

Other Variances

According to the hashie project page on GitHub (https://github.com/intridea/hashie), there are other types in the Hashie library for some special cases. I haven’t got a chance to use these, but I think it would be handy when the right use cases come.

Trash: is a Dash but it allows to translate keys upon initialization.
Clash: a Chainable Lazy Hash that allows you to easily construct complex hashes using method notation chaining.
Rash: a Hash whose keys can be Regexps or Ranges, which will map many input keys to a value.

Categories: Software Development Tags:
  1. Ian Bibby
    January 28th, 2015 at 16:54 | #1

    Just don’t ever use it in production (apparently):


  2. January 28th, 2015 at 17:23 | #2

    Thanks a lot Ian, really helpful information and nice reading there. Agree with you on the comment when it comes to heavy duty server side applications where performance and memory consumption are big concerns. For others, it may be OK, for example, command lines. In these cases, I value code readability and dev productivity more than perf/mem. :)


  1. No trackbacks yet.