diff options
| author | Jeremy Evans <code@jeremyevans.net> | 2025-05-30 18:25:58 -0700 |
|---|---|---|
| committer | Jeremy Evans <code@jeremyevans.net> | 2025-06-06 01:24:05 +0900 |
| commit | 0b07d2a1e32a456fc302c8d970fa85782bdb98ce (patch) | |
| tree | e8d65e3f767506fdebfdf3ba20c4dd74d946c70f /spec/ruby/core | |
| parent | 2f80117ce48f83a709989a0d88eb712b123ef371 (diff) | |
Deprecate passing arguments to Set#to_set and Enumerable#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.
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/13489
Diffstat (limited to 'spec/ruby/core')
| -rw-r--r-- | spec/ruby/core/enumerable/to_set_spec.rb | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/spec/ruby/core/enumerable/to_set_spec.rb b/spec/ruby/core/enumerable/to_set_spec.rb index 966baae1d9..d0fecf6de4 100644 --- a/spec/ruby/core/enumerable/to_set_spec.rb +++ b/spec/ruby/core/enumerable/to_set_spec.rb @@ -11,10 +11,21 @@ describe "Enumerable#to_set" do [1, 2, 3].to_set { |x| x * x }.should == Set[1, 4, 9] end - it "instantiates an object of provided as the first argument set class" do - set = [1, 2, 3].to_set(EnumerableSpecs::SetSubclass) - set.should be_kind_of(EnumerableSpecs::SetSubclass) - set.to_a.sort.should == [1, 2, 3] + ruby_version_is "3.5" do + it "instantiates an object of provided as the first argument set class" do + set = nil + proc{set = [1, 2, 3].to_set(EnumerableSpecs::SetSubclass)}.should complain(/Enumerable#to_set/) + set.should be_kind_of(EnumerableSpecs::SetSubclass) + set.to_a.sort.should == [1, 2, 3] + end + end + + ruby_version_is ""..."3.5" do + it "instantiates an object of provided as the first argument set class" do + set = [1, 2, 3].to_set(EnumerableSpecs::SetSubclass) + set.should be_kind_of(EnumerableSpecs::SetSubclass) + set.to_a.sort.should == [1, 2, 3] + end end it "does not need explicit `require 'set'`" do |
