Convert an array to a hash

Helo,

How to convert an array to a hash?
for example,

x

=> ["a", "hello", "b", "world", "c", "welcome", "d", "baby"]

to:

z

=> {"a"=>"hello", "b"=>"world", "c"=>"welcome", "d"=>"baby"}

And, for creating an array quickly, I could say:

x = %w/a hello b world c welcome d baby/

but how to creat a hash like this way?

Thanks.

Ruby Newbee wrote:

Helo,

How to convert an array to a hash?
for example,

x

=> ["a", "hello", "b", "world", "c", "welcome", "d", "baby"]

to:

z

=> {"a"=>"hello", "b"=>"world", "c"=>"welcome", "d"=>"baby"}

And, for creating an array quickly, I could say:

x = %w/a hello b world c welcome d baby/

but how to creat a hash like this way?

I'm not sure why you want to do this in the first place, but check out
the each_slice method.

Thanks.

Best,

···

--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
--
Posted via http://www.ruby-forum.com/\.

irb(main):001:0> a=["a", "hello", "b", "world", "c", "welcome", "d",
"baby"]
=> ["a", "hello", "b", "world", "c", "welcome", "d", "baby"]
irb(main):002:0> Hash[*a]
=> {"a"=>"hello", "b"=>"world", "c"=>"welcome", "d"=>"baby"}
irb(main):003:0>

···

---
http://zed.0xff.me
--
Posted via http://www.ruby-forum.com/.

Andrey Zaikin wrote:

irb(main):001:0> a=["a", "hello", "b", "world", "c", "welcome", "d",
"baby"]
=> ["a", "hello", "b", "world", "c", "welcome", "d", "baby"]
irb(main):002:0> Hash[*a]
=> {"a"=>"hello", "b"=>"world", "c"=>"welcome", "d"=>"baby"}
irb(main):003:0>

---
http://zed.0xff.me

Oh, right. I thought Hash had a constructor like that, but I couldn't
find it in the docs. Thanks!

Best,

···

--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
--
Posted via http://www.ruby-forum.com/\.

Thanks.
What's the "*" before "a" then?

···

2009/12/5 Andrey Zaikin <zed.0xff@gmail.com>:

irb(main):001:0> a=["a", "hello", "b", "world", "c", "welcome", "d",
"baby"]
=> ["a", "hello", "b", "world", "c", "welcome", "d", "baby"]
irb(main):002:0> Hash[*a]

It's the splat operator. It's used to "unarray" an array into its
individual items to pass them to a method:

irb(main):007:0> def m(a,b,c)
irb(main):008:1> p a
irb(main):009:1> p b
irb(main):010:1> p c
irb(main):011:1> end
=> nil
irb(main):012:0> x = [1,2,3]
=> [1, 2, 3]
irb(main):013:0> m *x
1
2
3

or to collect individual elements into an array when it appears in a lvalue:

irb(main):014:0> a,*b = [1,2,3,4,5]
=> [1, 2, 3, 4, 5]
irb(main):015:0> a
=> 1
irb(main):016:0> b
=> [2, 3, 4, 5]

or argument list of a method:

irb(main):020:0> def m(a,*b)
irb(main):021:1> p a
irb(main):022:1> p b
irb(main):023:1> end
=> nil
irb(main):024:0> m(1,2,3,4,5,6)
1
[2, 3, 4, 5, 6]

Hope this helps,

Jesus.

···

On Sat, Dec 5, 2009 at 9:22 AM, Ruby Newbee <rubynewbee@gmail.com> wrote:

2009/12/5 Andrey Zaikin <zed.0xff@gmail.com>:

irb(main):001:0> a=["a", "hello", "b", "world", "c", "welcome", "d",
"baby"]
=> ["a", "hello", "b", "world", "c", "welcome", "d", "baby"]
irb(main):002:0> Hash[*a]

Thanks.
What's the "*" before "a" then?

Ah, I got it, thanks~

···

2009/12/5 Jesús Gabriel y Galán <jgabrielygalan@gmail.com>:

On Sat, Dec 5, 2009 at 9:22 AM, Ruby Newbee <rubynewbee@gmail.com> wrote:

2009/12/5 Andrey Zaikin <zed.0xff@gmail.com>:

irb(main):001:0> a=["a", "hello", "b", "world", "c", "welcome", "d",
"baby"]
=> ["a", "hello", "b", "world", "c", "welcome", "d", "baby"]
irb(main):002:0> Hash[*a]

Thanks.
What's the "*" before "a" then?

It's the splat operator. It's used to "unarray" an array into its
individual items to pass them to a method: