Ruby Array to Hash (a better way?)

S

skibud2

Hey Guys,

I want to convert an array like the following to a hash. Below is my
current code (I am assuming there is a better way).

Thanks

["foo", "bar", "foo1", "bar1","foo2", "bar2"] convert to
{"foo"=>"bar", "foo1"=>"bar1","foo2"=>"bar2"}



My Current Code:

def array_to_hash(array)
count = 0
hash = Hash.new
(array.length / 2).times do
hash[array[count]] = array[count+1]
count += 2
end
return hash
end
 
G

Gordon Thiesfeld

Hey Guys,

I want to convert an array like the following to a hash. Below is my
current code (I am assuming there is a better way).

Thanks

["foo", "bar", "foo1", "bar1","foo2", "bar2"] convert to
{"foo"=>"bar", "foo1"=>"bar1","foo2"=>"bar2"}

Use Hash#[]
Hash["foo", "bar", "foo1", "bar1","foo2", "bar2"]
=> {"foo1"=>"bar1", "foo2"=>"bar2", "foo"=>"bar"}

hth

Gordon
 
M

Marcin Mielżyński

skibud2 pisze:
Hey Guys,

I want to convert an array like the following to a hash. Below is my
current code (I am assuming there is a better way).

Thanks

["foo", "bar", "foo1", "bar1","foo2", "bar2"] convert to
{"foo"=>"bar", "foo1"=>"bar1","foo2"=>"bar2"}

given:
a = ["foo", "bar", "foo1", "bar1","foo2", "bar2"]

puts Hash[*a]

or:

require 'enumerator'
h = {}
a.each_slice(2){|k,v|h[k]=v}
puts h


lopex
 
K

Kyle Schmitt

I've been using Ruby for awhile, and I'm afraid I've never used the *
notation...

I can see that Hash[a] wouldn't work, and that Hash[*a] expands the
array out so that Hash can use it elegantly.

But what, in a general, does the * do?

Or is it just for special cases like this?

Thanks,
Kyle
 
S

Sebastian Hungerecker

Kyle said:
But what, in a general, does the * do?

It turns an array into a list of parameters (or, if used in a method
definition, a list of parameters into an array).


HTH,
Sebastian
 
D

dblack

Hi --

I've been using Ruby for awhile, and I'm afraid I've never used the *
notation...

I can see that Hash[a] wouldn't work, and that Hash[*a] expands the
array out so that Hash can use it elegantly.

But what, in a general, does the * do?

Or is it just for special cases like this?

It "un-arrays" its operand; in other words, given an array, the *
gives you a list.


David

--
* Books:
RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
& consulting: Ruby Power and Light, LLC (http://www.rubypal.com)
 
R

Robert Klemme

skibud2 pisze:
Hey Guys,

I want to convert an array like the following to a hash. Below is my
current code (I am assuming there is a better way).

Thanks

["foo", "bar", "foo1", "bar1","foo2", "bar2"] convert to
{"foo"=>"bar", "foo1"=>"bar1","foo2"=>"bar2"}

given:
a = ["foo", "bar", "foo1", "bar1","foo2", "bar2"]

puts Hash[*a]

or:

require 'enumerator'
h = {}
a.each_slice(2){|k,v|h[k]=v}
puts h

If you are using Enumerator anyway you can as well do

irb(main):001:0> a = ["foo", "bar", "foo1", "bar1","foo2", "bar2"]
=> ["foo", "bar", "foo1", "bar1", "foo2", "bar2"]
irb(main):002:0> a.to_enum:)each_slice, 2).inject({}) {|h,(k,v)| h[k]=v; h}
=> {"foo1"=>"bar1", "foo2"=>"bar2", "foo"=>"bar"}
irb(main):003:0>

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top