capper
capper

Reputation: 29

No implicit conversion of nil into String - ruby

I get the error "No implicit conversion of nil into String." and I just can't find the error.

Here are the lines that are messing with me.

def sumprogram
  softd = IO.readlines("softdrinks.txt").map! {|s| s.to_i}
  beers = IO.readlines("beers.txt").map! {|s| s.to_i}
  drink = IO.readlines("drinks.txt").map! {|s| s.to_i}
  softdrinks = puts softd.sum
  beers = puts beers.sum
  drink = puts drink.sum
  puts "\n\nBeverages consumed thus far "
  puts " " + softdrinks + "\t - \t " + beers + "\t - " + drink + "."
end

It's the last line, before the end that is the problem. The tables im trying to display are the individuals sums of different arrays.

Upvotes: 2

Views: 4796

Answers (1)

Cyzanfar
Cyzanfar

Reputation: 7146

puts returns nil so in your code here you are assigning nil to local variables sofdrinks, beers and drink.

Here is what you need to do:

def sumprogram
      softd = IO.readlines("softdrinks.txt").map! {|s| s.to_i}
      beers = IO.readlines("beers.txt").map! {|s| s.to_i}
      drink = IO.readlines("drinks.txt").map! {|s| s.to_i}
      softdrinks = softd.sum
      beers = beers.sum
      drink = drink.sum
      puts "\n\nBeverages consumed thus far "
      puts " " + softdrinks + "\t - \t " + beers + "\t - " + drink + "."
end

Upvotes: 1

Related Questions