Project

General

Profile

Actions

Bug #22610

open

Array#eql?, #- and #& ignore a redefined #eql? of Integer, Float, Symbol, nil, true and false

Bug #22610: Array#eql?, #- and #& ignore a redefined #eql? of Integer, Float, Symbol, nil, true and false

Added by sferik (Erik Berlin) about 18 hours ago. Updated about 18 hours ago.

Status:
Open
Assignee:
-
Target version:
-
ruby -v:
ruby 4.1.0dev (2026-11-19T13:09:31Z master 1e71e987c2) +PRISM [arm64-darwin27]
[ruby-core:127631]

Description

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:

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?:

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:

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:

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.

Updated by sferik (Erik Berlin) about 18 hours ago Actions #1 [ruby-core:127632]

I've opened a pull request with a fix: https://github.com/ruby/ruby/pull/20026

It adds a BOP_EQL basic operator for the classes that the fast path handles and checks it instead of BOP_EQ when the fast path is answering for #eql?. Like the change that registered #== for nil, true and false, it defines #eql? in Integer, Symbol, NilClass, TrueClass and FalseClass, so nil.method(:eql?).owner becomes NilClass.

Actions

Also available in: PDF Atom