Ruby-like Hash in C# (sharp)

Scott StultsFebruary 5, 2008

One of the things I like about ruby is that you get more done with less typing, and though c# is a powerful language, I’ve been wishing for a ruby like hash and even array initializers. I’ve been spending some time doing an ruby on rail’s active record port to c#, and lately i’ve been porting over the rail’s migration ( I even have a prototype working with SQL 2005). However, one thing heavily missing from the c# recipe is the Ruby like Hash for doing migrations. So after some research on the web, I found Alex Henderdon & Andrey Shchekin’s ruby like hash function for c#. However this didn’t exactly take this concept all the way as it could have, so after some tweaks to a Dictionary>string, object< class, simple ruby like hash for C# now exists and works fairly nicely, as you can see from the code example below. (Taken from the tests project).



     Hash h = Hash.New(Name => "Michael Herndon", Age => 10, Url => "");
     h["Name"].ShouldBe("Michael Herndon");

I’ve also packed up Andrew Peter’s Inflector.Net using C#’s mixins. Infector.Net is based off of the class rail uses to do things like camelize, pluralize etc. Also I’ve put together some other rails like mixins like .Gsup, .Merge, .Each. Collect, .Join, for people to enjoy. If you want to take a peak at code, its on the amplify-net googlecode project inside the folder Framework/Amplify.Linq folder.

     svn checkout amplify-net

     url: <a href=""></a>

More blog articles:

We've been Solr-istas since day one!

Our founder wrote the first book on Solr, now in 3rd edition. We've helped organizations from the US Patent and Trademark Office to Cisco build smarter search solutions with Solr.

Learn More about our Solr services.