Forum: Ruby Ruby Array to Hash (a better way?)

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.
2953c93b1319b25c96c8cec6fc442d2d?d=identicon&s=25 Mike Halloran (foobarmike)
on 2007-08-01 19:32
(Received via mailing list)
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
14c657815683cad8a59e299c625c93c3?d=identicon&s=25 Marcin MielżyÅ?ski (lopex)
on 2007-08-01 19:36
(Received via mailing list)
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
Ec07fbda510a142f5331ffd903b07820?d=identicon&s=25 Gordon Thiesfeld (Guest)
on 2007-08-01 19:38
(Received via mailing list)
On Aug 1, 12:26 pm, skibud2 <mike.hallo...@gmail.com> wrote:
> 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
9dec3df8319c613f6f4f14a27da0fdb4?d=identicon&s=25 Kyle Schmitt (Guest)
on 2007-08-01 21:10
(Received via mailing list)
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
7a561ec0875fcbbe3066ea8fe288ec77?d=identicon&s=25 Sebastian Hungerecker (Guest)
on 2007-08-01 21:14
(Received via mailing list)
Kyle Schmitt wrote:
> 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
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 unknown (Guest)
on 2007-08-01 21:15
(Received via mailing list)
Hi --

On Thu, 2 Aug 2007, Kyle Schmitt wrote:

> 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
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2007-08-02 00:01
(Received via mailing list)
On 01.08.2007 19:34, Marcin Mielżyński wrote:
>>
> 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
This topic is locked and can not be replied to.