In Perl, to do a hash update based on arrays of keys and values, I can do something like:
@hash{'key1','key2','key3'} = ('val1','val2','val3');
In Ruby, I could do something like this in a more complex way:
hash.merge!(Hash[ *[['key1','key2','key3'],['val1','val2','val3']].transpose ])
OK, but I doubt the effectiveness of such a procedure.
Now I would like to make a more complex assignment in one line.
Perl example:
(@hash{'key1','key2','key3'}, $key4) = &some_function();
I have no idea if this is possible in some simple Ruby way. Any clues?
For the vulnerable Perl, @hash{'key1','key2','key3'} = ('a', 'b', 'c') is a hash fragment and is a shorthand for something like this:
$hash{'key1'} = 'a'; $hash{'key2'} = 'b'; $hash{'key3'} = 'c';
geronime
source share