Skip to content

Commit

Permalink
[GR-45621] Promote Set class to core library
Browse files Browse the repository at this point in the history
PullRequest: truffleruby/4135
  • Loading branch information
andrykonchin authored and eregon committed Feb 6, 2024
2 parents 92ed4d9 + 1aedc2a commit 677ac08
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
6 changes: 6 additions & 0 deletions spec/ruby/core/enumerable/fixtures/classes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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|
Expand Down
5 changes: 5 additions & 0 deletions src/main/ruby/truffleruby/core/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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)
Expand Down

0 comments on commit 677ac08

Please sign in to comment.