How to save a hash into a CSV

前端 未结 7 1557
名媛妹妹
名媛妹妹 2020-12-08 04:26

I am new in ruby so please forgive the noobishness.

I have a CSV with two columns. One for animal name and one for animal type. I have a hash with all the keys being

7条回答
  •  [愿得一人]
    2020-12-08 04:42

    Try this:

    require 'csv'
    data = { 'one' => '1', 'two' => '2', 'three' => '3' }
    
    CSV.open("data.csv", "a+") do |csv|
            csv << data.keys
            csv << data.values
    end
    

提交回复
热议问题