将字符串保存到文件 [英] save string to file

查看:149
本文介绍了将字符串保存到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个迭代(在我看来):

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

一个字符串。 @bigtable有来自迭代的每个@result。
现在我想要的:
我想将内容从@bigtable(点击一个按钮后)保存到.csv文件(并选择将其保存在我的hdd上)。我想做它像@bigtable的每个@result是在其自己的单行,像这样(伪代码):

  @result string from @bigtable [0] 
@resig字符串from @bigtable [1]

请帮助

解决方案

要将数组转换为字符串,您可以:

  @ bigtable.join(\\\

将此字符串写入文件:

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

p>

BTW:

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


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

解决方案

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]

这篇关于将字符串保存到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆