diff options
author | Akinori MUSHA <[email protected]> | 2020-12-22 12:20:21 +0900 |
---|---|---|
committer | Akinori MUSHA <[email protected]> | 2020-12-22 12:20:21 +0900 |
commit | 3fa4bd82928983cf5f6711c130711502397e05e2 (patch) | |
tree | db80a406ef9497d1e53ac26c43ffa275edf2e61e | |
parent | 63b872c409ec441718f55c60362a441cf108cfc0 (diff) |
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
-rw-r--r-- | lib/set.rb | 8 | ||||
-rw-r--r-- | lib/set/set.gemspec (renamed from lib/set.gemspec) | 5 | ||||
-rw-r--r-- | lib/set/sorted_set.rb | 6 | ||||
-rw-r--r-- | test/fixtures/fake_sorted_set_gem/sorted_set.rb | 3 | ||||
-rw-r--r-- | test/test_set.rb | 5 | ||||
-rw-r--r-- | test/test_sorted_set.rb | 45 |
6 files changed, 70 insertions, 2 deletions
diff --git a/lib/set.rb b/lib/set.rb index 625046d37c..2bd2a0ffa5 100644 --- a/lib/set.rb +++ b/lib/set.rb @@ -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" diff --git a/lib/set.gemspec b/lib/set/set.gemspec index 72dee127e1..e258af4978 100644 --- a/lib/set.gemspec +++ b/lib/set/set.gemspec @@ -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 = ["[email protected]"] 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. diff --git a/lib/set/sorted_set.rb b/lib/set/sorted_set.rb new file mode 100644 index 0000000000..e4e01ba4a8 --- /dev/null +++ b/lib/set/sorted_set.rb @@ -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 diff --git a/test/fixtures/fake_sorted_set_gem/sorted_set.rb b/test/fixtures/fake_sorted_set_gem/sorted_set.rb new file mode 100644 index 0000000000..02c9721fa2 --- /dev/null +++ b/test/fixtures/fake_sorted_set_gem/sorted_set.rb @@ -0,0 +1,3 @@ +class SortedSet + # ... +end diff --git a/test/test_set.rb b/test/test_set.rb index 05431e4c63..e62f30d852 100644 --- a/test/test_set.rb +++ b/test/test_set.rb @@ -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) diff --git a/test/test_sorted_set.rb b/test/test_sorted_set.rb new file mode 100644 index 0000000000..1ac6ee929a --- /dev/null +++ b/test/test_sorted_set.rb @@ -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 |