save string to file

末鹿安然 提交于 2019-12-09 06:24:33

问题


I've got a iteration (in my view):

(1..@count.to_i).each do |i|
   ...doing things...
  @bigtable << @result[0..result.length-2]
end

Every @result is a string. @bigtable has every @result from iterations. Now what I want: I want to save content from the @bigtable (after clickin a button) to .csv file (and choose where to save it on my hdd). And I want do it like every @result from @bigtable is in its own single line, like this (pseudo code):

@result string from @bigtable[0]
@result string from @bigtable[1]
etc.

Please, help


回答1:


To transform your array into a string you can do :

@bigtable.join("\n")

To write this string into a file :

File.open("path/to/file", "w") { |file| file.write @bigtable.join("\n") }

And that's it!

BTW:

@result[0..result.length-2] == @result[0..-2]


来源:https://stackoverflow.com/questions/8743131/save-string-to-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!