Home Ruby读写csv文件
Post
Cancel

Ruby读写csv文件

无语了,最近总遇到这个操作。发现自己从来没记录过。

所以写一下吧。

  def save_to_csv(csv)
    CSV.open("C:/ruby/logs.csv", "w") do |row|
      row << %w[name location dishes time]
    end
  end

  def self.read_from_csv(csv)
     CSV.foreach("C:/ruby/logs.csv") do |row|
     puts row ['name', 'location', 'dishes', 'time']
   end
 end
This post is licensed under CC BY 4.0 by the author.