Ruby Hashie Library

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.

Bothered by SLOW Web UI to manage vSphere? Want to manage ALL your VMware vCenters, AWS, Azure, Openstack, container behind a SINGLE pane of glass? Want to search, analyze, report, visualize VMs, hosts, networks, datastores, events as easily as Google the Web? Find out more about vSearch 3.0: the search engine for all your private and public clouds.

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 =
puts bookhash[:title]
puts bookmash.title
bookmash.isbn = "0137153635"
puts bookmash.isbn
freemash = = "AWS"

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.title = "VMware VI and vSphere SDK" = "Steve Jin"
book.isbn = "0137153635"
book.year = 2009
book.url = "" # => 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 =
bookmash.title = "VMware VI and vSphere SDK - 1st Edition"
book = true))
puts book.title

Other Variances

According to the hashie project page on GitHub (, 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.

This entry was posted in Software Development and tagged . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.


  1. Ian Bibby
    Posted January 28, 2015 at 4:54 pm | Permalink

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

  2. Posted January 28, 2015 at 5:23 pm | Permalink

    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. :)


Post a Comment

Your email is never published nor shared. Required fields are marked *


You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


    My company has created products like vSearch ("Super vCenter"), vijavaNG APIs, EAM APIs, ICE tool. We also help clients with virtualization and cloud computing on customized development, training. Should you, or someone you know, need these products and services, please feel free to contact me: steve __AT__

    Me: Steve Jin, VMware vExpert who authored the VMware VI and vSphere SDK by Prentice Hall, and created the de factor open source vSphere Java API while working at VMware engineering. Companies like Cisco, EMC, NetApp, HP, Dell, VMware, are among the users of the API and other tools I developed for their products, internal IT orchestration, and test automation.