Forum: Ruby Hash.from_zipped_array

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
05be5d6610e2c3f1780aa0e39e902e93?d=identicon&s=25 Farrel Lifson (Guest)
on 2006-03-01 03:21
(Received via mailing list)
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
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-03-01 03:40
(Received via mailing list)
On Wed, 1 Mar 2006, Farrel Lifson wrote:

>
> 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
     ---
     cat: meow
     bird: cheep
     dog: woof


kind regards.

-a
05be5d6610e2c3f1780aa0e39e902e93?d=identicon&s=25 Farrel Lifson (Guest)
on 2006-03-01 20:40
(Received via mailing list)
Is there anything they haven't thought of in the API? They still keep
suprising me...
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-03-01 21:31
(Received via mailing list)
Hi --

On Wed, 1 Mar 2006, ara.t.howard@noaa.gov wrote:

>    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.


David

--
David A. Black (dblack@wobblini.net)
Ruby Power and Light (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-03-01 21:54
(Received via mailing list)
On Mar 1, 2006, at 2:31 PM, dblack@wobblini.net wrote:

>>
>>    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
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-03-01 22:12
(Received via mailing list)
Hi --

On Thu, 2 Mar 2006, James Edward Gray II wrote:

>>>   require 'yaml' and y Hash[*animal.zip(sound).flatten]
>> 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}

I may be in the minority, but I prefer:

   Hash[*arr.flatten_once]

:-)


David

--
David A. Black (dblack@wobblini.net)
Ruby Power and Light (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black
7264fb16beeea92b89bb42023738259d?d=identicon&s=25 Christian Neukirchen (Guest)
on 2006-03-01 22:36
(Received via mailing list)
dblack@wobblini.net writes:

>>
>>    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*.
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-03-01 23:32
(Received via mailing list)
On Thu, 2 Mar 2006, Christian Neukirchen wrote:

>>>    require 'yaml' and y Hash[*animal.zip(sound).flatten]
>> levels, so that you can use that technique even with nested arrays.
>
> Try to get that into 2.0, at least flatten_once. *please*.

agreed.  however i'd strongly go with an api like


-a
0ec4920185b657a03edf01fff96b4e9b?d=identicon&s=25 Yukihiro Matsumoto (Guest)
on 2006-03-02 00:25
(Received via mailing list)
Hi,

In message "Re: Hash.from_zipped_array"
    on Thu, 2 Mar 2006 06:12:08 +0900, dblack@wobblini.net writes:

|I may be in the minority, but I prefer:
|
|   Hash[*arr.flatten_once]
|
|:-)

I'm not sure above is the best solution, but anyway giving #flatten a
level argument could be useful, so that you can do:

   Hash[*arr.flatten(1)]

It's more general, and even shorter.

							matz.
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-03-02 00:46
(Received via mailing list)
Hi --

On Thu, 2 Mar 2006, Yukihiro Matsumoto wrote:

>
> I'm not sure above is the best solution, but anyway giving #flatten a
> level argument could be useful, so that you can do:
>
>   Hash[*arr.flatten(1)]
>
> It's more general, and even shorter.

Actually the flattenx extension has both: flatten_once and flatten(n)
for any n.  I can't remember why I included flatten_once -- I guess
because it seemed to be the most common case and I liked giving it its
own name.


David

--
David A. Black (dblack@wobblini.net)
Ruby Power and Light (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2006-03-02 01:06
(Received via mailing list)
ara.t.howard@noaa.gov wrote:
> On Thu, 2 Mar 2006, Christian Neukirchen wrote:
...
>> 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?)
This topic is locked and can not be replied to.