未分類

ruby 2つの配列を合体させて、ハッシュを作る

配列["a","b","c"]と[1,2,3]を合体させて、ハッシュ{"a"=>1, "b"=>2, "c"=>3}
を作成する。

irb(main):001:0> h=["a","b","c"]
=> ["a", "b", "c"]
irb(main):002:0> g=[1,2,3]
=> [1, 2, 3]
irb(main):003:0> i=0
=> 0
irb(main):004:0> b={}
=> {}
irb(main):005:0> h.each do |n|
irb(main):006:1* b[n]=g[i]
irb(main):007:1> i+=1
irb(main):008:1> end
=> ["a", "b", "c"]
irb(main):009:0>
irb(main):010:0* b
=> {"a"=>1, "b"=>2, "c"=>3}
irb(main):011:0>

-未分類