Thanks for this but I am getting like
[[300, 1], [300, 2], [301, 1], [301, 3], [301, 4], [302, 3], [302, 4]]
???
irb(main):004:0> hsh = Hash.new{|h,k| h[k]=}
=> {}
irb(main):005:0> keys.zip(values).each{|k,v| hsh[k] << v}
=> [[300, 1], [300, 2], [301, 1], [301, 3], [301, 4], [302, 3], [302, 4]]
irb(main):006:0> hsh
=> {300=>[1, 2], 301=>[1, 3, 4], 302=>[3, 4]}
but I actually need like
hsh = {"300" =>["1","2"],"301" => ["1","3","4"],"302"=>["3","4"]}
Btw, #zip without #each is more efficient:
irb(main):007:0> hsh = Hash.new{|h,k| h[k]=}
=> {}
irb(main):008:0> keys.zip(values) {|k,v| hsh[k] << v}
=> nil
irb(main):009:0> hsh
=> {300=>[1, 2], 301=>[1, 3, 4], 302=>[3, 4]}
Cheers
robert
···
On Mon, Feb 7, 2011 at 1:12 PM, Arihan Sinha <arihan_sinha@yahoo.com> wrote:
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/