[前][次][番号順一覧][スレッド一覧]

ruby-changes:64442

From: Akinori <ko1@a...>
Date: Tue, 22 Dec 2020 12:30:14 +0900 (JST)
Subject: [ruby-changes:64442] 3fa4bd8292 (master): Import set 1.0.0

https://git.ruby-lang.org/ruby.git/commit/?id=3fa4bd8292

From 3fa4bd82928983cf5f6711c130711502397e05e2 Mon Sep 17 00:00:00 2001
From: Akinori MUSHA <knu@i...>
Date: Tue, 22 Dec 2020 12:20:21 +0900
Subject: 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

diff --git a/lib/set.gemspec b/lib/set.gemspec
deleted file mode 100644
index 72dee12..0000000
--- a/lib/set.gemspec
+++ /dev/null
@@ -1,24 +0,0 @@ https://github.com/ruby/ruby/blob/trunk/#L0
-Gem::Specification.new do |spec|
-  spec.name          = "set"
-  spec.version       = "0.1.0"
-  spec.authors       = ["Akinori MUSHA"]
-  spec.email         = ["knu@i..."]
-
-  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.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
-
-  # 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.
-  spec.files         = Dir.chdir(File.expand_path('..', __FILE__)) do
-    `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
-  end
-  spec.bindir        = "exe"
-  spec.executables   = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
-  spec.require_paths = ["lib"]
-end
diff --git a/lib/set.rb b/lib/set.rb
index 625046d..2bd2a0f 100644
--- a/lib/set.rb
+++ b/lib/set.rb
@@ -641,6 +641,12 @@ class Set https://github.com/ruby/ruby/blob/trunk/lib/set.rb#L641
     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 https://github.com/ruby/ruby/blob/trunk/lib/set.rb#L690
     klass.new(self, *args, &block)
   end
 end
+
+autoload :SortedSet, "#{__dir__}/set/sorted_set"
diff --git a/lib/set/set.gemspec b/lib/set/set.gemspec
new file mode 100644
index 0000000..e258af4
--- /dev/null
+++ b/lib/set/set.gemspec
@@ -0,0 +1,25 @@ https://github.com/ruby/ruby/blob/trunk/lib/set/set.gemspec#L1
+Gem::Specification.new do |spec|
+  spec.name          = "set"
+  spec.version       = "1.0.0"
+  spec.authors       = ["Akinori MUSHA"]
+  spec.email         = ["knu@i..."]
+
+  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.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.
+  spec.files         = Dir.chdir(File.expand_path('..', __FILE__)) do
+    `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
+  end
+  spec.bindir        = "exe"
+  spec.executables   = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
+  spec.require_paths = ["lib"]
+end
diff --git a/lib/set/sorted_set.rb b/lib/set/sorted_set.rb
new file mode 100644
index 0000000..e4e01ba
--- /dev/null
+++ b/lib/set/sorted_set.rb
@@ -0,0 +1,6 @@ https://github.com/ruby/ruby/blob/trunk/lib/set/sorted_set.rb#L1
+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 0000000..02c9721
--- /dev/null
+++ b/test/fixtures/fake_sorted_set_gem/sorted_set.rb
@@ -0,0 +1,3 @@ https://github.com/ruby/ruby/blob/trunk/test/fixtures/fake_sorted_set_gem/sorted_set.rb#L1
+class SortedSet
+  # ...
+end
diff --git a/test/test_set.rb b/test/test_set.rb
index 05431e4..e62f30d 100644
--- a/test/test_set.rb
+++ b/test/test_set.rb
@@ -759,6 +759,11 @@ class TC_Set < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/test_set.rb#L759
     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 0000000..1ac6ee9
--- /dev/null
+++ b/test/test_sorted_set.rb
@@ -0,0 +1,45 @@ https://github.com/ruby/ruby/blob/trunk/test/test_sorted_set.rb#L1
+# 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
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]