没有将nil隐式转换为String - ruby

时间:2017-10-11 23:15:44

标签: arrays ruby

我收到错误"没有将nil隐式转换为String。"我无法找到错误。

以下是与我混淆的线条。

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

这是最后一行,在问题结束之前。我试图显示的表是不同数组的个体总和。

1 个答案:

答案 0 :(得分:0)

puts返回nil因此,在您的代码中,您将nil分配给局部变量sofdrinksbeersdrink

以下是您需要做的事情:

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