Ruby返回TypeError:无法将Object转换为String
我开始使用Ruby,我有以下模块:
module RadioreportHelper @totalsum = 0 @radio @daysAndTotals def load(service, transactionsPerDay) puts "#{service} : #{transactionsPerDay}" @radio = service @daysAndTotals = transactionsPerDay transactionsPerDay.each{|day, total| @totalsum += total } end attr_reader :radio, :daysAndTotals, :totalsum end
我正在运行以下unit testing用例:
class RadioreportHelperTest 20, 2=>30, 4=>40, 5=>50} radioReport = RadioreportHelper.load(service, transactionsPerDay) assert_not_nil(radioReport) assert_equal("Mix", radioReport.radio.name) end end
但我总是得到以下错误:TypeError:无法将Service转换为String
我希望服务对象位于模块RadioreportHelper中并将其存储在@radio变量而不是字符串中。 谢谢,所有的帮助家伙!
尝试将to_s
方法添加到您的服务模型。
def to_s service # or some other method in the model that returns a string end
没有必要从插值表达式内部调用to_s
,即"#{service}"
将返回service.to_s
的结果。
编辑
别介意这一切。 您的RadioreportHelper.load
方法未被访问 – 而是从ActiveSupport :: Dependencies :: Loadable获得load
。 尝试将load
方法重命名为其他方法。
(我讨厌名字冲突。)
你一定要试试这个:
puts "#{service.to_s} : #{transactionsPerDay}"
虽然我不确定如何在字符串中处理哈希插值,因此您可能需要使用
puts "#{service.to_s} : #{transactionsPerDay.to_s}"