如何在Ruby中将字符串保存到CSV文件中?

如何在Ruby中将字符串保存到CSV文件中?,ruby,csv,Ruby,Csv,我已经写了代码,但它不会工作。代码是: #Parse and print the Tweet if the response code was 200 tweets = nil if response.code == '200' then tweets = JSON.parse(response.body) require 'csv' CSV.open("trial.csv", "ab") do |csv| csv << ["text", "created_at", "name

我已经写了代码,但它不会工作。代码是:

 #Parse and print the Tweet if the response code was 200
tweets = nil
if response.code == '200' then
tweets = JSON.parse(response.body)
require 'csv'
CSV.open("trial.csv", "ab") do |csv|
  csv << ["text", "created_at", "name", 'id']
  csv << ["tweets"]
  end
end
#如果响应代码为200,则解析并打印Tweet
tweets=nil
如果response.code==“200”,则
tweets=JSON.parse(response.body)
需要“csv”
CSV.open(“trial.CSV”、“ab”)do | CSV|

csv您的代码的问题是您正在打印字符串“tweets”,而我相信您的意思是打印变量
tweets
的值。因此,需要做的更改是删除“tweets”
周围的双引号:

CSV.open(“trial.CSV”、“ab”)do | CSV|

你尝试过谷歌搜索吗?我正在认真地抵制因缺乏努力而否决这项提案的冲动。该死,我肯定有人会这么做,因为我已经把它放在那里了。
CSV.open("trial.csv", "ab") do |csv|
  csv << ["text", "created_at", "name", 'id']
  csv << [tweets]  # <- NOTE the change here 
  end
end