no implicit conversion of nil into String error

Angela Jonon

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

#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.

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:

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?

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:

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(', ')

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

TypeError: no implicit conversion of nil into String error

From Dev

ActionView::Template::Error (no implicit conversion of nil into String)

From Dev

Rails: no implicit conversion of nil into String

From Dev

Rails: no implicit conversion of nil into String

From Dev

TypeError: no implicit conversion of nil into String

From Dev

TypeError: no implicit conversion of nil into String with gsub

From Dev

No implicit conversion of nil into String. dbview_cti

From Dev

Rails, mongoid throws TypeError no implicit conversion of nil into String

From Dev

Cannot update Calabash-sandbox Ruby "no implicit conversion of nil into String"

From Dev

TypeError in MessagesController#create_message no implicit conversion of nil into String

From Dev

Ruby bug with fresh install (no implicit conversion of nil into String)

From Dev

no implicit conversion of nil into String when using hash without =>

From Dev

eRuby - no implicit conversion of nil into String when comparing numbers in an if statement

From Dev

Controller spec .reload error: no implicit conversion of nil into Hash

From Dev

Nested Attributes Error: no implicit conversion of String into Integer

From Dev

RSpec - Type error - no implicit conversion of String into Integer

From Dev

ActionView::Template::Error (no implicit conversion of String into Integer)

From Dev

Ruby error - '+': no implicit conversion of Fixnum into String (TypeError)

From Dev

Ruby Error: "no implicit conversion of Fixnum into String"

From Dev

Nested Attributes Error: no implicit conversion of String into Integer

From Dev

no implicit conversion of nil into integer with ruby

From Dev

No implicit conversion of Array into String?

From Dev

No implicit conversion of file into string

From Dev

No implicit conversion of Array into String?

From Dev

implicit conversion string to integer

From Dev

Why implicit conversion of bool to string isn't an error?

From Dev

Rails 4 - "No Implicit Conversion of Fixnum into String" - Error only occurs in production

From Dev

Client Side Validations creating "no implicit conversion of Fixnum into String" error

From Dev

How to solve a JSON parse error: 'no implicit conversion of String into Integrer'?

Related Related

  1. 1

    TypeError: no implicit conversion of nil into String error

  2. 2

    ActionView::Template::Error (no implicit conversion of nil into String)

  3. 3

    Rails: no implicit conversion of nil into String

  4. 4

    Rails: no implicit conversion of nil into String

  5. 5

    TypeError: no implicit conversion of nil into String

  6. 6

    TypeError: no implicit conversion of nil into String with gsub

  7. 7

    No implicit conversion of nil into String. dbview_cti

  8. 8

    Rails, mongoid throws TypeError no implicit conversion of nil into String

  9. 9

    Cannot update Calabash-sandbox Ruby "no implicit conversion of nil into String"

  10. 10

    TypeError in MessagesController#create_message no implicit conversion of nil into String

  11. 11

    Ruby bug with fresh install (no implicit conversion of nil into String)

  12. 12

    no implicit conversion of nil into String when using hash without =>

  13. 13

    eRuby - no implicit conversion of nil into String when comparing numbers in an if statement

  14. 14

    Controller spec .reload error: no implicit conversion of nil into Hash

  15. 15

    Nested Attributes Error: no implicit conversion of String into Integer

  16. 16

    RSpec - Type error - no implicit conversion of String into Integer

  17. 17

    ActionView::Template::Error (no implicit conversion of String into Integer)

  18. 18

    Ruby error - '+': no implicit conversion of Fixnum into String (TypeError)

  19. 19

    Ruby Error: "no implicit conversion of Fixnum into String"

  20. 20

    Nested Attributes Error: no implicit conversion of String into Integer

  21. 21

    no implicit conversion of nil into integer with ruby

  22. 22

    No implicit conversion of Array into String?

  23. 23

    No implicit conversion of file into string

  24. 24

    No implicit conversion of Array into String?

  25. 25

    implicit conversion string to integer

  26. 26

    Why implicit conversion of bool to string isn't an error?

  27. 27

    Rails 4 - "No Implicit Conversion of Fixnum into String" - Error only occurs in production

  28. 28

    Client Side Validations creating "no implicit conversion of Fixnum into String" error

  29. 29

    How to solve a JSON parse error: 'no implicit conversion of String into Integrer'?

HotTag

Archive