convert an array to a hash

R

Ruby Newbee

Helo,

How to convert an array to a hash?
for example,
=> ["a", "hello", "b", "world", "c", "welcome", "d", "baby"]

to:
=> {"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.
 
M

Marnen Laibow-Koser

Ruby said:
Helo,

How to convert an array to a hash?
for example,
=> ["a", "hello", "b", "world", "c", "welcome", "d", "baby"]

to:
=> {"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.


Best,
-- 
Marnen Laibow-Koser
http://www.marnen.org
(e-mail address removed)
 
A

Andrey Zaikin

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

Marnen Laibow-Koser

Andrey said:
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>

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
(e-mail address removed)
 
R

Ruby Newbee

2009/12/5 Andrey Zaikin said:
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?
 
J

Jesús Gabriel y Galán

2009/12/5 Andrey Zaikin said:
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:

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

Ruby Newbee

2009/12/5 Jes=C3=BAs Gabriel y Gal=C3=A1n said:
2009/12/5 Andrey Zaikin said:
irb(main):001:0> a=3D["a", "hello", "b", "world", "c", "welcome", "d",
"baby"]
=3D> ["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:

Ah, I got it, thanks~
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top