From: "sferik (Erik Berlin) via ruby-core" Date: 2026-11-19T18:51:26+00:00 Subject: [ruby-core:127631] [Ruby Bug#22610] Array#eql?, #- and #& ignore a redefined #eql? of Integer, Float, Symbol, nil, true and false Issue #22610 has been reported by sferik (Erik Berlin). ---------------------------------------- Bug #22610: Array#eql?, #- and #& ignore a redefined #eql? of Integer, Float, Symbol, nil, true and false https://bugs.ruby-lang.org/issues/22610 * Author: sferik (Erik Berlin) * Status: Open * ruby -v: ruby 4.1.0dev (2026-11-19T13:09:31Z master 1e71e987c2) +PRISM [arm64-darwin27] * Backport: 3.3: UNKNOWN, 3.4: UNKNOWN, 4.0: UNKNOWN ---------------------------------------- On master, methods that compare elements with `eql?` in C, such as `Array#eql?`, `Array#-` and `Array#&`, don't call a redefined `#eql?` when the receiver is an Integer, a Float, a Symbol, `nil`, `true`, `false` or a String: ```ruby class Integer alias_method :orig_eql?, :eql? def eql?(other) = other.nil? || orig_eql?(other) end [1].eql?([nil]) #=> false on master, true on Ruby 3.3, 3.4 and 4.0 [1] & [nil] #=> [] on master, [1] on Ruby 3.3, 3.4 and 4.0 ``` The same happens with `NilClass#eql?`, `TrueClass#eql?`, `Symbol#eql?` (for static and dynamic Symbols) and `Float#eql?`: ```ruby class NilClass def eql?(other) = true end [nil].eql?([:a]) #=> false on master, true before [nil, 1] - [:a] #=> [nil, 1] on master, [1] before ``` I expect the redefined method to be called, as it is when the receiver is any other object. These go through `rb_eql()`, which shares a fast path with `rb_equal()`. The fast path answers without calling the method when it can tell from the VALUEs alone, but it only checks whether `#==` has been redefined. Redefining `#==` in any of those classes makes `eql?` work again, which shows where the check is: ```ruby class Integer alias_method :orig_eq, :== def ==(other) = orig_eq(other) end class NilClass def eql?(other) = true end [nil].eql?([:a]) #=> true ``` Before https://github.com/ruby/ruby/commit/652cf512535acee456a56b4781c8100dcfb276ef, the fast path only handled two Fixnums, two flonums, a static Symbol, two Floats and two Strings, so only those pairs ignored `#eql?`. That part goes back further, to at least Ruby 3.3: ```ruby class Integer def eql?(other) = true end [1].eql?([2]) #=> false on Ruby 3.3 through master ``` That commit extended the fast path to every immediate receiver compared with anything other than a Fixnum or flonum, and to dynamic Symbols. So the receivers above now skip `#eql?` whatever they are compared with, not only when compared with their own class. | | 3.3 | 3.4 | 4.0 | master | |:--|:--:|:--:|:--:|:--:| | `[1].eql?([nil])` with `Integer#eql?` redefined | true | true | true | false | | `[nil].eql?([:a])` with `NilClass#eql?` redefined | true | true | true | false | | `[true].eql?([false])` with `TrueClass#eql?` redefined | true | true | true | false | | `[1.5].eql?([nil])` with `Float#eql?` redefined | true | true | true | false | | `["dyn_a".to_sym].eql?(["dyn_b".to_sym])` with `Symbol#eql?` redefined | true | true | true | false | | `[1].eql?([2])` with `Integer#eql?` redefined | false | false | false | false | | `["a"].eql?(["b"])` with `String#eql?` redefined | false | false | false | false | All of the expected results are true. -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/