1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Import set 1.0.0

- SortedSet has been removed for dependency and performance reasons.
- Set#join is added as a shorthand for `.to_a.join`.
- Set#<=> is added.

https://github.com/ruby/set/blob/v1.0.0/CHANGELOG.md
This commit is contained in:
Akinori MUSHA 2020-12-22 12:20:21 +09:00
parent 63b872c409
commit 3fa4bd8292
6 changed files with 70 additions and 2 deletions

View file

@ -641,6 +641,12 @@ class Set
end
end
# Returns a string created by converting each element of the set to a string
# See also: Array#join
def join(separator=nil)
to_a.join(separator)
end
InspectKey = :__inspect_key__ # :nodoc:
# Returns a string containing a human-readable representation of the
@ -684,3 +690,5 @@ module Enumerable
klass.new(self, *args, &block)
end
end
autoload :SortedSet, "#{__dir__}/set/sorted_set"

View file

@ -1,17 +1,18 @@
Gem::Specification.new do |spec|
spec.name = "set"
spec.version = "0.1.0"
spec.version = "1.0.0"
spec.authors = ["Akinori MUSHA"]
spec.email = ["knu@idaemons.org"]
spec.summary = %q{Provides a class to deal with collections of unordered, unique values}
spec.description = %q{Provides a class to deal with collections of unordered, unique values}
spec.homepage = "https://github.com/ruby/set"
spec.license = "BSD-2-Clause"
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
spec.licenses = ["Ruby", "BSD-2-Clause"]
spec.metadata["homepage_uri"] = spec.homepage
spec.metadata["source_code_uri"] = spec.homepage
spec.metadata["changelog_uri"] = "https://github.com/ruby/set/blob/v#{spec.version}/CHANGELOG.md"
# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.

6
lib/set/sorted_set.rb Normal file
View file

@ -0,0 +1,6 @@
begin
require 'sorted_set'
rescue ::LoadError
raise "The `SortedSet` class has been extracted from the `set` library." \
"You must use the `sorted_set` gem or other alternatives."
end

View file

@ -0,0 +1,3 @@
class SortedSet
# ...
end

View file

@ -759,6 +759,11 @@ class TC_Set < Test::Unit::TestCase
assert_equal Set[1,2,3], set1
end if Kernel.instance_method(:initialize_clone).arity != 1
def test_join
assert_equal('123', Set[1, 2, 3].join)
assert_equal('1 & 2 & 3', Set[1, 2, 3].join(' & '))
end
def test_inspect
set1 = Set[1, 2]
assert_equal('#<Set: {1, 2}>', set1.inspect)

45
test/test_sorted_set.rb Normal file
View file

@ -0,0 +1,45 @@
# frozen_string_literal: false
require 'test/unit'
require 'set'
class TC_SortedSet < Test::Unit::TestCase
def base_dir
"#{__dir__}/../lib"
end
def assert_runs(ruby, options: nil)
options = ['-I', base_dir, *options]
r = system(RbConfig.ruby, *options, '-e', ruby)
assert(r)
end
def test_error
assert_runs <<~RUBY
require "set"
r = begin
puts SortedSet.new
rescue Exception => e
e.message
end
raise r unless r.match? /has been extracted/
RUBY
end
def test_ok_with_gem
assert_runs <<~RUBY, options: ['-I', "#{__dir__}/fixtures/fake_sorted_set_gem"]
require "set"
var = SortedSet.new.to_s
RUBY
end
def test_ok_require
assert_runs <<~RUBY, options: ['-I', "#{__dir__}/fixtures/fake_sorted_set_gem"]
require "set"
require "sorted_set"
var = SortedSet.new.to_s
RUBY
end
end