Skip to content

Commit

Permalink
Import set 1.0.0
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
knu committed Dec 22, 2020
1 parent 63b872c commit 3fa4bd8
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 2 deletions.
8 changes: 8 additions & 0 deletions lib/set.rb
Expand Up @@ -641,6 +641,12 @@ def tsort_each_child(node, &block)
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
Expand Down Expand Up @@ -684,3 +690,5 @@ def to_set(klass = Set, *args, &block)
klass.new(self, *args, &block)
end
end

autoload :SortedSet, "#{__dir__}/set/sorted_set"
5 changes: 3 additions & 2 deletions lib/set.gemspec → 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 = ["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.
Expand Down
6 changes: 6 additions & 0 deletions 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
3 changes: 3 additions & 0 deletions test/fixtures/fake_sorted_set_gem/sorted_set.rb
@@ -0,0 +1,3 @@
class SortedSet
# ...
end
5 changes: 5 additions & 0 deletions test/test_set.rb
Expand Up @@ -759,6 +759,11 @@ def test_freeze_clone_false
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)
Expand Down
45 changes: 45 additions & 0 deletions 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

0 comments on commit 3fa4bd8

Please sign in to comment.