Flattening an array is a standard question in interviews, especially for modern languages like python and ruby. I just thought trying to flatten a hash would be much more interesting.

Here is my attempt at converting something like

{“a”=>1, “b”=>{“c”=>3}, “b2″=>{“c2″=>{“d2″=>2, “d3″=>8, “b2″=>9}}}

into

{“a”=>1, [“b2”, “c2”]=>9, [“b2”, “c2”, “d2”]=>2, [“b2”, “c2”, “d3”]=>8, [“b”, “c”]=>3}

require 'enumerator'
class Hash
 def flat_each(prefix=[], &block)
      each do |key, value|
           if value.is_a?(Hash)
                 value.flat_each(prefix + [key], &block)
           else
                 yield prefix + [key], value
           end
     end
end

main_hash = Hash.new
given = {"a"  => 1,"b"  => { "c" => 3 },"b2" => {"c2" => {"d2" => 2,"d3"  => 8,"b2"  => 9}}}
given.to_enum(:flat_each).collect { |k,v| main_hash.merge!({ (k.size == 1) ?  k.first : k.uniq  => v}) }
puts main_hash

Algorithm adapted from Ruby Forum.
Not sure this is what you would call flattening a hash, but this is the best I could rustle up in half an hour.

So yesterday, I was trying to do a simple MySQL query and realised that the value I wanted was of type text and the field I wanted was actually in the middle of the string and not always consistently there. So I decided to write up a script for myself which would do the DB query and then parse the string returned. As I had not done python for sometime, I decided to write it in python.

First things first, all I did was import MySQLdb. Nothing! Wasn’t installed. Fair enough. I had never had to previously install a python module, so I went to sourceforge and obtained the source, built it and it ran. That got me thinking, why do I have to do this myself?

I tried using go and even with the same import (mysql;), I needed to download the mysql module from google code and build it with the 8g and 8l binaries and then have a crack at it. Why am I still building these ???

If I wanted to do this in Ruby, I could use the gem installer and simply do a gem list -r mysql to find out which one was available and then do a simple gem install mysql-X.Y. No need to build. Installed and ready for use.

I like python and go a lot, but I believe for simplicity of use and for mimicking the behaviour of apt-get, ruby wins this one.

Of the languages that I have used in my professional career, Ruby is the one that makes testing easy and fun. It has an easy to understand testing framework and mocking out external classes, most of the time, is very simple. For example:

class Sauron
__def wants
____"the ring"
__end
end

def Gandalf
__def wants
____"peace"
__end
end

class MiddleEarth
__def who_wants_what
____if (Gandalf.wants == peace and Sauron.wants == "the ring") then
______return "war"
____else
______return "peace"
____end
__end
end

Lets say that we have to test out Gandalf or Sauron.
Gandalf.wants.should eql "peace"
Sauron.wants.should eql "the ring"

The above test code is called RSpec and is very simple and easy to understand. Whether it is a Business Analyst or a Project Manager or even your mother, she can fully understand the required behaviour or Gandalf or Sauron.

But what if we had to unit test MiddleEarth? Then we would have to mock Gandalf and Sauron together and alternately to test all code paths. RSpec makes that easy.
Gandalf.should_receive(:wants).and_return("something")
Sauron.should_receive(:wants).and_return("something else")

If either method of either class expected arguments, that too we can deal with.
Sauron.should_receive(:wants).with(something).and_return("something else")

So there you go, testing and mocking made inherently easy with Ruby and its testing frameworks. But how does it compare to the old language of Perl. Perl too, now has a very similar testing framework to RSpec, called Test::Expectation. And for mocking out external influences when unit testing, we have Test::MockObject and Sub::Override.

And if we wanted to business test in either language, cucumber can sit on top of both, although inherently it works much more readily with Ruby/RSpec.