Skip to content

Deprecate passing arguments to Set#to_set and Enumerable#to_set#13489

Merged
jeremyevans merged 1 commit intoruby:masterfrom
jeremyevans:deprecate-to_set-args
Jun 5, 2025
Merged

Deprecate passing arguments to Set#to_set and Enumerable#to_set#13489
jeremyevans merged 1 commit intoruby:masterfrom
jeremyevans:deprecate-to_set-args

Conversation

@jeremyevans
Copy link
Copy Markdown
Contributor

Array#to_a, Hash#to_h, Enumerable#to_a, and Enumerable#to_h do not allow you to specify subclasses. This has undesired behavior when passing non-Set subclasses. All of these are currently allowed, and none make sense:

enum = [1,2,3].to_enum

enum.to_set(Hash)
enum.to_set(Struct.new("A", :a))
enum.to_set(ArgumentError)
enum.to_set(Thread){}

Users who want to create instances of a subclass of Set from an enumerable should pass the enumerable to SetSubclass.new instead of using to_set.

Array#to_a, Hash#to_h, Enumerable#to_a, and Enumerable#to_h do not
allow you to specify subclasses.  This has undesired behavior when
passing non-Set subclasses.  All of these are currently allowed, and
none make sense:

```ruby
enum = [1,2,3].to_enum

enum.to_set(Hash)
enum.to_set(Struct.new("A", :a))
enum.to_set(ArgumentError)
enum.to_set(Thread){}
```

Users who want to create instances of a subclass of Set from an
enumerable should pass the enumerable to SetSubclass.new instead of
using to_set.
@nobu nobu requested a review from knu June 1, 2025 06:51
Copy link
Copy Markdown
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.

As it turned out, subclassing the Set class did not become very common, so I think it's fine to deprecate this feature now.

@jeremyevans jeremyevans merged commit 0b07d2a into ruby:master Jun 5, 2025
86 checks passed
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