Project

General

Profile

Actions

Bug #21008

closed

Array#sum, Enumerator#sum, Numeric subclass

Added by MSP-Greg (Greg L) 9 days ago. Updated 1 day ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 3.5.0dev (2025-01-06T17:07:57Z master a61c16ba42) +PRISM [x86_64-linux]
[ruby-core:120512]

Description

The following code seems to show a bug or 'quirk'. When using a Numeric subclass, Array#sum works correctly, but Enumerator#sum does not. They both work fine when using a Float.

class Length < Numeric
  def initialize(val)
    @val = Float(val)
  end

  def to_f
    @val
  end
end

ary = []

#────────────────────────────────────────── Use Length

ary[0] = Length.new  5
ary[1] = Length.new 10
ary[2] = Length.new 20

puts "Length ary  #{ary.sum}"   #=> 35.0

enum = ary.each
puts "Length enum #{enum.sum}"  #=> 20.0

#────────────────────────────────────────── Use Float

ary[0] =  5.0
ary[1] = 10.0
ary[2] = 20.0

puts "Float  ary  #{ary.sum}"   #=> 35.0

enum = ary.each
puts "Float  enum #{enum.sum}"  #=> 35.0

Updated by nobu (Nobuyoshi Nakada) 9 days ago

  • Backport changed from 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN to 3.1: REQUIRED, 3.2: REQUIRED, 3.3: REQUIRED, 3.4: REQUIRED
Actions #2

Updated by nobu (Nobuyoshi Nakada) 9 days ago

  • Status changed from Open to Closed

Applied in changeset git|b176d4f52e4af67654814dab3e9c5f4bf9170e54.


[Bug #21008] Normalize before sum to float

After switching to Float-mode when summing Numeric objects,
normalization for Float is still needed.

Updated by nagachika (Tomoyuki Chikanaga) 5 days ago

  • Backport changed from 3.1: REQUIRED, 3.2: REQUIRED, 3.3: REQUIRED, 3.4: REQUIRED to 3.1: REQUIRED, 3.2: DONE, 3.3: REQUIRED, 3.4: REQUIRED

Updated by k0kubun (Takashi Kokubun) 1 day ago

  • Backport changed from 3.1: REQUIRED, 3.2: DONE, 3.3: REQUIRED, 3.4: REQUIRED to 3.1: REQUIRED, 3.2: DONE, 3.3: DONE, 3.4: REQUIRED

ruby_3_3 commit:7b9caf19ba480d168ef4c5e93690735240975c91 merged revision(s) b176d4f52e4af67654814dab3e9c5f4bf9170e54.

Actions

Also available in: Atom PDF

Like0
Like0Like1Like0Like0