Skip to content

Conversation

@kyanagi
Copy link
Contributor

@kyanagi kyanagi commented Nov 29, 2024

Suppose we define MySet as a subclass of Set.
The methods MySet#&, MySet#|, and MySet#- all return an instance of MySet, but only MySet#^ returns an instance of Set.
Wouldn't it be better if ^ also returned an instance of MySet?

When Set was introduced in Ruby 1.8.0, the implementation up to 1.8.5 returned an instance of MySet.
This behavior changed at the following commit: a4104d6.

irb(main):001> class MySet < Set; end
=> nil
irb(main):002> ms = MySet[1, 2]
=> #<MySet: {1, 2}>
irb(main):003> s = Set[1, 3]
=> #<Set: {1, 3}>
irb(main):004> ms & s
=> #<MySet: {1}>
irb(main):005> ms | s
=> #<MySet: {1, 2, 3}>
irb(main):006> ms - s
=> #<MySet: {2}>
irb(main):007> ms ^ s
=> #<Set: {3, 2}>

@kyanagi kyanagi requested a review from knu as a code owner November 29, 2024 02:19
Copy link
Member

@knu knu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that was intentional. Thanks.

@knu knu merged commit 51f5111 into ruby:master Nov 29, 2024
11 checks passed
@kyanagi kyanagi deleted the fix-subclass-xor branch November 29, 2024 10:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants