Forum: Ruby array to hash

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.
9c7e3e773ac08cb6e7ae2f4f7eeadaa6?d=identicon&s=25 Servando Garcia (Guest)
on 2007-03-20 12:33
(Received via mailing list)
<html><head><meta name="Generator" content="PSI HTML/CSS Generator"/>
<style type="text/css"><!--
body{font-family:'Tahoma';font-size:10pt;font-color:'#000000';}
LI{display:list-item;margin:0.00in;}
p{display:block;margin:0.00in;}
body{}
--></style>
</head><BODY ><div><SPAN
style="font-family:'Arial';font-size:10pt;">Hello all</SPAN></div>
<div><SPAN style="font-family:'Arial';font-size:10pt;">&nbsp; &nbsp; Is
there a method to collect the items in an array into a hash</SPAN></div>
<div>&nbsp;</div>
<div><SPAN style="font-family:'Arial';font-size:10pt;">I want to go from
this X =[1,2,3,4,5,6] to this Y={1 =&gt;2, 3 =&gt;4, 5
=&gt;6}</SPAN></div>
<div>&nbsp;</div>
<div><SPAN style="font-family:'Arial';font-size:10pt;">Just seems like
there should be one</SPAN></div>
<div>&nbsp;</div>
<div><SPAN style="font-family:'Arial';font-size:10pt;">Thanks in
advance</SPAN></div>
<div>&nbsp;</div>
<div><SPAN style="font-family:'Arial';font-size:10pt;">Sam</SPAN></div>
</body></html>
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 David A. Black (Guest)
on 2007-03-20 12:43
(Received via mailing list)
Hi --

On 3/20/07, Servando Garcia <garcia.servando@gmail.com> wrote:
>
> Hello all
>     Is there a method to collect the items in an array into a hash
>
> I want to go from this X =[1,2,3,4,5,6] to this Y={1 =>2, 3 =>4, 5 =>6}

Hash[*X]


David
4371f8fa1c3574e74d8f88c77d23e7d7?d=identicon&s=25 Harry Ha (harry)
on 2007-03-20 12:47
(Received via mailing list)
On 3/20/07, Servando Garcia <garcia.servando@gmail.com> wrote:
> Sam
Try this


arr = [1,2,3,4,5,6]
ahash = Hash[*arr]
p arr
p ahash

Harry

--

http://www.kakueki.com/ruby/list.html
Japanese Ruby List Subjects in English
9c7e3e773ac08cb6e7ae2f4f7eeadaa6?d=identicon&s=25 Servando Garcia (Guest)
on 2007-03-20 12:51
(Received via mailing list)
<html><head><meta name="Generator" content="PSI HTML/CSS Generator"/>
<style type="text/css"><!--
body{font-family:'Tahoma';font-size:10pt;font-color:'#000000';}
LI{display:list-item;margin:0.00in;}
p{display:block;margin:0.00in;}
body{}
--></style>
</head><BODY ><div><SPAN style="font-size:10pt;">Guess I should have
googled harder/longer I have found some code that appears to work. Now
I'll have to spend sometime stepping through it.</SPAN></div>
<div><SPAN style="font-size:10pt;">Here is the code:</SPAN></div>
<div>&nbsp;</div>
<div style="margin-top:0.07in;"><SPAN style="font-family:'Times New
Roman';font-size:12pt;">class Array<br />def to_h(&amp;block)<br
/>Hash[*self.collect { |v|<br />[v, block.call(v)]<br />}.flatten]<br
/>end<br />end</SPAN></div>
<div><SPAN style="font-size:10pt;">&#160;</SPAN></div>
<div>&nbsp;</div>
<div><SPAN style="font-size:10pt;">and the link to the
page:</SPAN></div>
<div><SPAN style="font-size:10pt;"><a
href="http://snippets.dzone.com/posts/show/302">http://s...
<div>&nbsp;</div>
<div><SPAN style="font-size:10pt;">Harold, Please forgive my
impatience</SPAN></div>
<div>&nbsp;</div>
<div><SPAN style="font-size:10pt;">Sam<br /><br /></span></div>
<div><SPAN style="font-size:10pt;color:navy;">&gt; Hello
all</SPAN></div>
<div><SPAN style="font-size:10pt;color:navy;">&gt; &#160;  Is there a
method to collect the items in an array into a hash</SPAN></div>
<div><SPAN style="font-size:10pt;color:navy;">&gt;</SPAN></div>
<div><SPAN style="font-size:10pt;color:navy;">&gt; I want to go from
this X =[1,2,3,4,5,6] to this Y={1 =&gt;2, 3 =&gt;4, 5</SPAN></div>
<div><SPAN style="font-size:10pt;color:navy;">&gt; =&gt;6}</SPAN></div>
<div><SPAN style="font-size:10pt;color:navy;">&gt;</SPAN></div>
<div><SPAN style="font-size:10pt;color:navy;">&gt; Just seems like there
should be one</SPAN></div>
<div><SPAN style="font-size:10pt;color:navy;">&gt;</SPAN></div>
<div><SPAN style="font-size:10pt;color:navy;">&gt; Thanks in
advance</SPAN></div>
<div><SPAN style="font-size:10pt;color:navy;">&gt;</SPAN></div>
<div><SPAN style="font-size:10pt;color:navy;">&gt; Sam</SPAN></div>
</body></html>
9c7e3e773ac08cb6e7ae2f4f7eeadaa6?d=identicon&s=25 Servando Garcia (Guest)
on 2007-03-20 12:53
(Received via mailing list)
<html><head><meta name="Generator" content="PSI HTML/CSS Generator"/>
<style type="text/css"><!--
body{font-family:'Tahoma';font-size:10pt;font-color:'#000000';}
LI{display:list-item;margin:0.00in;}
p{display:block;margin:0.00in;}
--></style>
</head><BODY ><div><SPAN style="font-size:10pt;">David </SPAN></div>
<div><SPAN style="font-size:10pt;">&nbsp; &nbsp; No Way that is to easy.
My new question to you is how does this work. Where can I read the
source code for this method<br /><br /></span></div>
<div><SPAN style="font-size:10pt;color:navy;">&gt; Hi --</SPAN></div>
<div><SPAN style="font-size:10pt;color:navy;">&gt;</SPAN></div>
<div><SPAN style="font-size:10pt;color:navy;">&gt; On 3/20/07, Servando
Garcia wrote:</SPAN></div>
<div><SPAN style="font-size:10pt;color:navy;">&gt;</SPAN></div>
<div><SPAN style="font-size:10pt;color:maroon;">&gt;&gt; Hello
all</SPAN></div>
<div><SPAN style="font-size:10pt;color:maroon;">&gt;&gt; Is there a
method to collect the items in an array into a hash</SPAN></div>
<div><SPAN style="font-size:10pt;color:maroon;">&gt;&gt;</SPAN></div>
<div><SPAN style="font-size:10pt;color:maroon;">&gt;&gt; I want to go
from this X =[1,2,3,4,5,6] to this Y={1 =&gt;2, 3 =&gt;4,</SPAN></div>
<div><SPAN style="font-size:10pt;color:maroon;">&gt;&gt; 5
=&gt;6}</SPAN></div>
<div><SPAN style="font-size:10pt;color:maroon;">&gt;&gt;</SPAN></div>
<div><SPAN style="font-size:10pt;color:navy;">&gt; Hash[*X]</SPAN></div>
<div><SPAN style="font-size:10pt;color:navy;">&gt;</SPAN></div>
<div><SPAN style="font-size:10pt;color:navy;">&gt;</SPAN></div>
<div><SPAN style="font-size:10pt;color:navy;">&gt; David</SPAN></div>
</body></html>
8a85c693f13ef7cb542ef94d2a403d4d?d=identicon&s=25 Luc Heinrich (Guest)
on 2007-03-20 15:29
(Received via mailing list)
On 20 mars 07, at 12:53, Servando Garcia wrote:

> Where can I read the source code for this method ?

<http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/hash.c?
revision=11708&view=markup>

The function you are looking for is 'rb_hash_s_create'.
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 David A. Black (Guest)
on 2007-03-20 15:37
(Received via mailing list)
Hi --

On 3/20/07, Servando Garcia <garcia.servando@gmail.com> wrote:
>
> David
>     No Way that is to easy. My new question to you is how does this work.

The 'unarray' operator * turns an array into a list.  So in effect
you're doing:

  Hash[1,2,3,4,5,6]


David
Ae16cb4f6d78e485b04ce1e821592ae5?d=identicon&s=25 Martin DeMello (Guest)
on 2007-03-20 15:40
(Received via mailing list)
On 3/20/07, Servando Garcia <garcia.servando@gmail.com> wrote:
>
> David
>     No Way that is to easy. My new question to you is how does this work.
> Where can I read the source code for this method

It's a combination of two things:

1. Hash.[] creates a hash from a comma separated even-length list

Hash[1,2,3,4,5,6] # => { 1=>2, 3=>4, 5=>6 }

2. *ary converts an array into a comma-separated list

def foo(a, b=nil, c=nil)
  p a
  p b
  p c
end

ary = [1,2,3]

def foo(a, b=nil, c=nil)
  p a
  p b
  p c
end

ary = [1,2,3]

irb> foo(ary)
[1, 2, 3]
nil
nil
=> nil
irb> foo(*ary)
1
2
3
=> nil

martin
Fd22ee3cfc7dac283ce8e451af324f7d?d=identicon&s=25 Chad Perrin (Guest)
on 2007-03-20 21:32
(Received via mailing list)
On Tue, Mar 20, 2007 at 08:33:09PM +0900, Servando Garcia wrote:
> <div><SPAN style="font-family:'Arial';font-size:10pt;">I want to go from this X 
=[1,2,3,4,5,6] to this Y={1 =&gt;2, 3 =&gt;4, 5 =&gt;6}</SPAN></div>
> <div>&nbsp;</div>
> <div><SPAN style="font-family:'Arial';font-size:10pt;">Just seems like there should be 
one</SPAN></div>
> <div>&nbsp;</div>
> <div><SPAN style="font-family:'Arial';font-size:10pt;">Thanks in advance</SPAN></div>
> <div>&nbsp;</div>
> <div><SPAN style="font-family:'Arial';font-size:10pt;">Sam</SPAN></div>
> </body></html>
>

I'm glad you got the answer you were seeking.  I have a request, though:

Please provide plain text email content in the future.  Some of us
(intentionally) avoid HTML email.
This topic is locked and can not be replied to.