So this is a trivial, but hopefully fun question. I need to make a Hash
with identical keys and values from the keys of an existing Hash
. Here's an example input along with my best attempt so far:
input = {'cat' => 'meow', 'dog' => nil}
Hash[*input.keys.map {|k| [k,k]}.flatten]
#=> {'cat' => 'cat', 'dog' => 'dog'}
I don't think this is particularly readable, so I was wondering if there was a better, more expressive syntax for doing this in Ruby, particularly one that might be more readable for future programmers who maintain the code?
This is how I would do the same thing in Python, and I find it to be slightly more readable:
dict([[a,a] for a in input])
But that could just be because I'm used to reading Python!
Looking for suggestions that will work with Ruby 1.8.6, which is the version I am constrained to.