ruby 没有将 nil 隐式转换为 String 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27817679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 06:39:58  来源:igfitidea点击:

no implicit conversion of nil into String error

rubyimplicit-conversion

提问by Angela Jonon

I have a ruby script that will create two files by taking and merging values from another file.

我有一个 ruby​​ 脚本,它将通过从另一个文件中获取和合并值来创建两个文件。

#Resources
require 'rubygems'
require 'csv'

col_date = []
col_constant1 = []
col_constant2 = []
col_appYear = []
col_statsDesc = []
col_keyStats =[]
col_weeklyTotal=[]


weekly_total = []

fname = "finalStats.csv" #variable for capture file
        finalStatsFile = File.open(fname, "w") #write to capture file
fname2 = "weeklyStats.csv"
        weeklyStatsFile = File.open(fname2, "w")    
CSV.foreach('compareData.csv', converters: :numeric) do |row|
    weekly_total << row[0] - row[1]

    weekly_total.each do |data| 
    data << weekly_total.shift 
     weeklyStatsFile.puts data
end 
end

#retrieve stats from original document 
 CSV.foreach("autoCapture.csv") {|row| col_date << row[0]}
 CSV.foreach("autoCapture.csv") {|row| col_constant1 << row[1]}
 CSV.foreach("autoCapture.csv") {|row| col_appYear << row[2]}
 CSV.foreach("autoCapture.csv") {|row| col_statsDesc << row[3]}
 CSV.foreach("autoCapture.csv") {|row| col_constant2 << row[4]}
 CSV.foreach("autoCapture.csv") {|row| col_keyStats << row[5]}
 CSV.foreach("weeklyStats.csv") {|row| col_weeklyTotal << row[0]}



  col_date.zip(col_constant1, col_appYear, col_statsDesc, col_constant2, col_keyStats, col_weeklyTotal).each do |col_date, col_constant1, col_appYear, col_statsDesc, col_constant2, 
  col_keyStats, col_weeklyTotal|

  finalStatsFile.puts col_date+", "+col_constant1+", "+ col_appYear+", "+col_statsDesc+", "+col_constant2+", "+col_keyStats+", "+col_weeklyTotal

 end

In one file I wish to subtract the values in row[1] from the values in row[0] to create a new 'weekly_total' value. I then output this array of values in a file called weeklyStats.csv. This will output a column of values fine.

在一个文件中,我希望从 row[0] 中的值中减去 row[1] 中的值以创建一个新的“weekly_total”值。然后我在一个名为weeklyStats.csv 的文件中输出这个值数组。这将输出一列值。

However, I want to join these values with another set from another file (autoCapture.csv) and when I try to zip them as arrays so that they read across in corresponding rows I get the error:

但是,我想将这些值与另一个文件 (autoCapture.csv) 中的另一组值连接起来,当我尝试将它们压缩为数组以便它们在相应的行中读取时,出现错误:

weeklyStats_csv.rb:42:in `+': no implicit conversion of nil into String (TypeError)
    from weeklyStats_csv.rb:42:in `block in <main>'
    from weeklyStats_csv.rb:40:in `each'
    from weeklyStats_csv.rb:40:in `<main>'

I gather this means that the array zip will not catch an exception if the one of the values is nil and therefore cannot convert to string. The problem is, I have tried converting weekly_total to string and array as I thought that it may be the problem (a mismatch of types) but I just dont where to go from here. Can anyone help?

我认为这意味着如果其中一个值为零,则数组 zip 将不会捕获异常,因此无法转换为字符串。问题是,我尝试将weekly_total 转换为字符串和数组,因为我认为这可能是问题所在(类型不匹配),但我只是不知道从哪里开始。任何人都可以帮忙吗?

回答by Aleksei Matiushkin

One of (or more) values in string

字符串中的一个(或多个)值

finalStatsFile.puts col_date+", "+col_constant1+", "+ col_appYear+", "+col_statsDesc+", "+col_constant2+", "+col_keyStats+", "+col_weeklyTotal

became nil. To fix the output you should explicitly cast them to strings:

变成了nil。要修复输出,您应该将它们显式转换为字符串:

finalStatsFile.puts col_date.to_s + ", " + 
                    col_constant1.to_s + ", " + 
                    col_appYear.to_s + ", " + 
                    col_statsDesc.to_s + ", " +
                    col_constant2.to_s + ", " +
                    col_keyStats.to_s + ", " + 
                    col_weeklyTotal.to_s 

BTW, the whole clause might be rewritten in more rubyish manner:

顺便说一句,整个条款可能会以更红宝石的方式重写:

finalStatsFile.puts [ col_date,
                      col_constant1,
                      col_appYear,
                      col_statsDesc,
                      col_constant2,
                      col_keyStats,
                      col_weeklyTotal ].map(&:to_s).join(', ')