Idiom for creating hash from two arrays

David A. Black wrote:

···

On Thu, 9 Dec 2004, Glenn Parker wrote:

This style of expression can only create a new hash instead of updating an existing hash. I think the "core" operation is updating/merging an existing hash with a set of matched keys and values.

hash.update(key_array.hash_with(value_array)) :slight_smile:

Unless Ruby is much smarter than I thought, this will build a new (albeit temporary) hash, then pass it to Hash#update. Not quite the same as updating hash in place, and less efficient in terms of memory usage.

The smiley suggests to me you already appreciate this distinction. :slight_smile:

--
Glenn Parker | glenn.parker-AT-comcast.net | <http://www.tetrafoil.com/&gt;

Hi --

David A. Black wrote:

This style of expression can only create a new hash instead of updating an existing hash. I think the "core" operation is updating/merging an existing hash with a set of matched keys and values.

hash.update(key_array.hash_with(value_array)) :slight_smile:

Unless Ruby is much smarter than I thought, this will build a new (albeit temporary) hash, then pass it to Hash#update. Not quite the same as updating hash in place, and less efficient in terms of memory usage.

The smiley suggests to me you already appreciate this distinction. :slight_smile:

Not exactly... more that I don't think updating an existing hash from
two arrays really needs its own semantics, beyond those provided by
Hash#update. I think the most common uses of Hash#update involve
hashes that already exist. So, for example, you might have:

   hash.update(Hash[*some_array.flatten])

which also probably isn't the most efficient possible way, but I'd
rather keep the question of optimizing Hash#update separate from the
question of creating hashes out of two arrays.

David

···

On Thu, 9 Dec 2004, Glenn Parker wrote:

On Thu, 9 Dec 2004, Glenn Parker wrote:

--
David A. Black
dblack@wobblini.net