AlexB52 · GitHub

This code returns true on Ruby v3.4.2 and used to work on mruby as well.

Color = Struct.new(:r, :g, :b) do
  def ==(other)
    super
  end
end
Color.new(1,2,3) == Color.new(1,2,3)
# => true

A regression was introduced at this commit 5ca2d44 on the 7th of July. Passed that point Color.new(1,2,3) == Color.new(1,2,3) starts to return false when overriding Struct#==

    mruby-struct: add recursion detection to Struct#== and Struct#eql?
    Prevent SystemStackError when comparing structs with circular
    references.  Uses the same recursion detection mechanism as Hash and
    Array equality methods.
    Co-authored-by: Claude <noreply@anthropic.com>
 mrbgems/mruby-struct/src/struct.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

This is a simplified example to isolate the issue. I discovered the bug because I overrode Struct#== with some extra logic before calling super and tests started to fail on a mruby update.

Note: Aliasing like alias old_equal :"==" doesn't work either.

EDIT: It looks like this is expected?
The main difference is that you are more likely to override Struct#== but not Hash#== or Array#== but maybe this is an edge case and a conservative behaviour that the developer needs to know and work around?

Read the original on github.com ↗