i've noticed..

2010, May 31

There must be an easier way..

.. to create multidimensional arrays in ruby.

Here’s the only solution I found that works the way I expected it to. This solution works but I have no clue as to how. So, I’m assuming there is an easier, more intuitive, way of doing it and I am asking you for your help.

hash_lambda = lambda { Hash.new {|h,k| h[k] = hash_lambda.call } }
teamOfficial = hash_lambda[]

teamOfficial[‘Minnesota’][‘ncaa’] = ‘Golden\ Gophers’

teamOfficial[‘Minnesota’][‘nfl’] = ‘Vikings’

teamOfficial[‘Minnesota’][‘nba’] = ‘Timberwolves’

My goal is to allow each element to have a unique combination of keys, even with redundant information and without brute forcing the data into one string for the key (i.e. ‘ncaaMinnesota’, ‘nbaMinnesota’). Storing an array inside of another is less than ideal for what I’m doing.