Skip to content

Commit

Permalink
Resurrect support for Ruby 2.x
Browse files Browse the repository at this point in the history
In Ruby 2.x, initialize_copy does not take a freeze option.
  • Loading branch information
knu committed Sep 20, 2020
1 parent a45625e commit 3da6c30
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
16 changes: 12 additions & 4 deletions lib/set.rb
Expand Up @@ -136,10 +136,18 @@ def initialize_dup(orig)
@hash = orig.instance_variable_get(:@hash).dup
end

# Clone internal hash.
def initialize_clone(orig, freeze: nil)
super
@hash = orig.instance_variable_get(:@hash).clone(freeze: freeze)
if Kernel.instance_method(:initialize_clone).arity != 1
# Clone internal hash.
def initialize_clone(orig, **options)
super
@hash = orig.instance_variable_get(:@hash).clone(**options)
end
else
# Clone internal hash.
def initialize_clone(orig)
super
@hash = orig.instance_variable_get(:@hash).clone
end
end

def freeze # :nodoc:
Expand Down
2 changes: 1 addition & 1 deletion test/test_set.rb
Expand Up @@ -739,7 +739,7 @@ def test_freeze_clone_false
set2.add 5
assert_equal Set[1,2,3,5], set2
assert_equal Set[1,2,3], set1
end
end if Kernel.instance_method(:initialize_clone).arity != 1

def test_inspect
set1 = Set[1, 2]
Expand Down

0 comments on commit 3da6c30

Please sign in to comment.