Array to hash

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}

Just seems like there should be one

Thanks in advance

Sam

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

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black\)
   (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf\)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)

Try this

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

Harry

···

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}

Just seems like there should be one

Thanks in advance

Sam

--

Japanese Ruby List Subjects in English

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.

Here is the code:

class Array
def to_h(&block)
Hash[*self.collect { |v|
[v, block.call(v)]
}.flatten]
end
end

and the link to the page:

http://snippets.dzone.com/posts/show/302

Harold, Please forgive my impatience

Sam

···

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}

Just seems like there should be one

Thanks in advance

Sam

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.

···

On Tue, Mar 20, 2007 at 08:33:09PM +0900, Servando Garcia wrote:

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

--
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
unix virus: If you're using a unixlike OS, please forward
this to 20 others and erase your system partition.

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

Hi –

On 3/20/07, Servando Garcia 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

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

The function you are looking for is 'rb_hash_s_create'.

···

On 20 mars 07, at 12:53, Servando Garcia wrote:

Where can I read the source code for this method ?

--
Luc Heinrich - luc@honk-honk.com - http://www.honk-honk.com

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

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black\)
   (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf\)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)

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]

foo(ary)

[1, 2, 3]
nil
=> nil

foo(*ary)

1
2
3
=> nil

martin

···

On 3/20/07, Servando Garcia <garcia.servando@gmail.com> wrote: