Skip to content

[GR-45621] Promote Set class to core library #3422

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -44,6 +44,7 @@ Compatibility:
* Do not autosplat a proc that accepts a single positional argument and keywords (#3039, @andrykonchin).
* Support passing anonymous * and ** parameters as method call arguments (#3039, @andrykonchin).
* Handle either positional or keywords arguments by default in `Struct.new` (#3039, @rwstauner).
* Promote `Set` class to core library (#3039, @andrykonchin).

Performance:

6 changes: 6 additions & 0 deletions spec/ruby/core/enumerable/fixtures/classes.rb
Original file line number Diff line number Diff line change
@@ -342,4 +342,10 @@ def ===(*args)
@block.call(*args)
end
end

# Set is a core class since Ruby 3.2
ruby_version_is '3.2' do
class SetSubclass < Set
end
end
end # EnumerableSpecs utility classes
29 changes: 29 additions & 0 deletions spec/ruby/core/enumerable/to_set_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require_relative '../../spec_helper'
require_relative 'fixtures/classes'

ruby_version_is "3.2" do
describe "Enumerable#to_set" do
it "returns a new Set created from self" do
[1, 2, 3].to_set.should == Set[1, 2, 3]
{a: 1, b: 2}.to_set.should == Set[[:b, 2], [:a, 1]]
end

it "passes down passed blocks" 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]
end

it "does not need explicit `require 'set'`" do
output = ruby_exe(<<~RUBY, options: '--disable-gems', args: '2>&1')
puts [1, 2, 3].to_set
RUBY

output.chomp.should == "#<Set: {1, 2, 3}>"
end
end
end
12 changes: 12 additions & 0 deletions spec/ruby/library/set/set_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require_relative '../../spec_helper'

describe 'Set' do
ruby_version_is '3.2' do
it 'is available without explicit requiring' do
output = ruby_exe(<<~RUBY, options: '--disable-gems', args: '2>&1')
puts Set.new([1, 2, 3])
RUBY
output.chomp.should == "#<Set: {1, 2, 3}>"
end
end
end
1 change: 1 addition & 0 deletions spec/tags/core/enumerable/to_set_tags.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
slow:Enumerable#to_set does not need explicit `require 'set'`
1 change: 1 addition & 0 deletions spec/tags/library/set/set_tags.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
slow:Set is available without explicit requiring
4 changes: 4 additions & 0 deletions src/main/ruby/truffleruby/core/enumerable.rb
Original file line number Diff line number Diff line change
@@ -332,6 +332,10 @@ def to_h(*arg)
h
end

def to_set(klass = Set, ...)
klass.new(self, ...)
end

# Synchronize with Enumerator#zip and Array#zip
def zip(*enums)
enums.map! do |enum|
5 changes: 5 additions & 0 deletions src/main/ruby/truffleruby/core/post.rb
Original file line number Diff line number Diff line change
@@ -112,3 +112,8 @@ def p(*a)
$LOAD_PATH.unshift(*extra_load_paths.map { |path| File.expand_path(path) })
end
end

# Set class is in the core library but it's still possible to require it as a standard
# library with `require`. So it doesn't make sense to have two copies of the same code
# and it's easier just to autoload CRuby's provided source file here.
autoload :Set, 'set'
5 changes: 5 additions & 0 deletions src/main/ruby/truffleruby/core/truffle/versioned_array.rb
Original file line number Diff line number Diff line change
@@ -655,6 +655,11 @@ def to_s(*args, &block)
copy.to_s(*args, &block)
end

def to_set(*args, &block)
copy = TruffleRuby.synchronized(@lock) { Array.new self }
copy.to_set(*args, &block)
end

def transpose(*args, &block)
copy = TruffleRuby.synchronized(@lock) { Array.new self }
copy.transpose(*args, &block)