我最近遇到了一个问题,大小数之和返回的不是大小数,而是整数.
sig { returns(BigDecimal) }
def period_hours
@applicable_items.sum { |payroll_item| payroll_item.hours_worked.to_d }
end
其中@applicable_items
返回PayrollItem
的array.
我得到的错误:
Return value: Expected type BigDecimal, got type Integer with value 0
此外,RubyMine还警告返回类型不兼容:
Incompatible types
Required:
BigDecimal
Returned:
Integer
我认为我可以很容易地通过在末尾添加to_d
来逃脱,就像这样:
@applicable_items.sum { |payroll_item| payroll_item.hours_worked.to_d }.to_d
但是,我仍然很好奇,为什么它可能返回Integer
而不是BigDecimal
,而总和的所有值都已经使用to_d
进行了转换.
非常感谢!