6
ruby使用transpose生成hash
source link: http://blog.grayson.org.cn/blog/2016/11/17/how-to-use-ruby-transpose-convert-to-hash
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
ruby使用transpose生成hash
Nov 17, 2016
irb(main):001:0> header = ['key1', 'key2', 'key3', 'key4']
=> ["key1", "key2", "key3", "key4"]
irb(main):002:0> values = ['val1', 'val2', 'val3', 'val4']
=> ["val1", "val2", "val3", "val4"]
irb(main):003:0> [header, values].transpose
=> [["key1", "val1"], ["key2", "val2"], ["key3", "val3"], ["key4", "val4"]]
irb(main):004:0> transpose_array = [header, values].transpose
=> [["key1", "val1"], ["key2", "val2"], ["key3", "val3"], ["key4", "val4"]]
irb(main):005:0> Hash[transpose_array]
=> {"key1"=>"val1", "key2"=>"val2", "key3"=>"val3", "key4"=>"val4"}
irb(main):006:0>
https://apidock.com/ruby/Array/transpose
http://railscasts.com/episodes/396-importing-csv-and-excel
此文章 短链接: http://dlj.bz/AbOLKI
Posted by grayson Nov 17, 2016 ruby skill
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK