trackoid icon indicating copy to clipboard operation
trackoid copied to clipboard

Ruby 2.0 regression on sum

Open JamesHarrison opened this issue 11 years ago • 1 comments

Not got time to dig into this one but after an update to Ruby 2.0.0-p0, calls like:

@image.views.last_days(7).sum

Throws Mongoid::Tracking::ReaderExtender can't be coerced into Fixnum.

@image.views.last_days(7).map{|v|v.to_f}.sum.to_i

Works as a hacky workaround - #to_i in the map function or #total both fail, to_f seems to work.

JamesHarrison avatar Mar 26 '13 02:03 JamesHarrison

I swapped "other + @total" in the first argument of the initializer and it resolved the issue for me.

  def +(other)
    return @total + other unless other.is_a?(ReaderExtender)
    self.class.new(other + @total, @hours.zip(other.hourly).map!(&:sum))
  end

carlson avatar Apr 12 '13 11:04 carlson