Hi folks,
Array has got a pretty cool method zip which 'zips' two arrays together like so:
[1,2,3].zip([2,4,6])
=> [[1,2],[2,4],[3,6]]
That's also what you get when you call to_a on a Hash like
{1=>2,2=>4,3=>6}, so I thought why not add a method to Hash to do the
reverse, that is to construct a Hash from a zipped array?
class Hash
def Hash.from_zipped_array(zipped_array)
zipped_array.inject({}) do |hash,key_value_pair|
hash[key_value_pair[0]] =key_value_pair[1]
hash
end
end
end
# Example usage
animal = ["dog","cat",bird"]
sound = ["woof,"meow","cheep"]
make_a_sound_like_a = Hash.from_zipped_array(animal.zip(sound)
make_a_sound_like_a["dog"]
=>"woof"
Useful enough for inclusion?
Farrel
it's pretty dang easy to do already:
harp:~ > cat a.rb
animal, sound = %w[dog cat bird], %w[woof meow cheep]
require 'yaml' and y Hash[*animal.zip(sound).flatten]
harp:~ > ruby a.rb
···
On Wed, 1 Mar 2006, Farrel Lifson wrote:
Hi folks,
Array has got a pretty cool method zip which 'zips' two arrays together like so:
[1,2,3].zip([2,4,6])
=> [[1,2],[2,4],[3,6]]
That's also what you get when you call to_a on a Hash like
{1=>2,2=>4,3=>6}, so I thought why not add a method to Hash to do the
reverse, that is to construct a Hash from a zipped array?
class Hash
def Hash.from_zipped_array(zipped_array)
zipped_array.inject({}) do |hash,key_value_pair|
hash[key_value_pair[0]] =key_value_pair[1]
hash
end
end
end
# Example usage
animal = ["dog","cat",bird"]
sound = ["woof,"meow","cheep"]
make_a_sound_like_a = Hash.from_zipped_array(animal.zip(sound)
make_a_sound_like_a["dog"]
=>"woof"
Useful enough for inclusion?
Farrel
---
cat: meow
bird: cheep
dog: woof
kind regards.
-a
--
judge your success by what you had to give up in order to get it.
- h.h. the 14th dali lama
Is there anything they haven't thought of in the API? They still keep
suprising me...
Hi --
it's pretty dang easy to do already:
harp:~ > cat a.rb
animal, sound = %w[dog cat bird], %w[woof meow cheep]
require 'yaml' and y Hash[*animal.zip(sound).flatten]
harp:~ > ruby a.rb
---
cat: meow
bird: cheep
dog: woof
A good opportunity for my annual plug for the flattenx extension 
(On RAA, still, I think.) It lets you flatten by any number of
levels, so that you can use that technique even with nested arrays.
It's not too hard to allow nested Arrays in Hash construction even without the library:
>> arr = [[:one, 1], [:two, %w{an Array}], [:three, 2]]
=> [[:one, 1], [:two, ["an", "Array"]], [:three, 2]]
>> Hash[*arr.inject(Array.new) { |args, a| args.push(*a) }]
=> {:two=>["an", "Array"], :three=>2, :one=>1}
James Edward Gray II
···
On Mar 1, 2006, at 2:31 PM, dblack@wobblini.net wrote:
On Wed, 1 Mar 2006, ara.t.howard@noaa.gov wrote:
dblack@wobblini.net writes:
Hi --
it's pretty dang easy to do already:
harp:~ > cat a.rb
animal, sound = %w[dog cat bird], %w[woof meow cheep]
require 'yaml' and y Hash[*animal.zip(sound).flatten]
harp:~ > ruby a.rb
---
cat: meow
bird: cheep
dog: woof
A good opportunity for my annual plug for the flattenx extension 
(On RAA, still, I think.) It lets you flatten by any number of
levels, so that you can use that technique even with nested arrays.
Try to get that into 2.0, at least flatten_once. *please*.
···
On Wed, 1 Mar 2006, ara.t.howard@noaa.gov wrote:
David
--
Christian Neukirchen <chneukirchen@gmail.com> http://chneukirchen.org
agreed. however i'd strongly go with an api like
-a
···
On Thu, 2 Mar 2006, Christian Neukirchen wrote:
dblack@wobblini.net writes:
Hi --
On Wed, 1 Mar 2006, ara.t.howard@noaa.gov wrote:
it's pretty dang easy to do already:
harp:~ > cat a.rb
animal, sound = %w[dog cat bird], %w[woof meow cheep]
require 'yaml' and y Hash[*animal.zip(sound).flatten]
harp:~ > ruby a.rb
---
cat: meow
bird: cheep
dog: woof
A good opportunity for my annual plug for the flattenx extension 
(On RAA, still, I think.) It lets you flatten by any number of
levels, so that you can use that technique even with nested arrays.
Try to get that into 2.0, at least flatten_once. *please*.
--
judge your success by what you had to give up in order to get it.
- h.h. the 14th dali lama
...
Try to get that into 2.0, at least flatten_once. *please*.
agreed. however i'd strongly go with an api like
-a
Is that the sound of one API clapping? 
Seriously, I cast my vote for flatten having an optional level argument.
(Couldn't this go in 1.8.5. since it wouldn't break anything?)
···
ara.t.howard@noaa.gov wrote:
On Thu, 2 Mar 2006, Christian Neukirchen wrote:
--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407