From d3706bde52607063eb04f9deb99e95f174ce5db3 Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune Date: Thu, 1 Oct 2020 23:08:49 -0400 Subject: [PATCH] Define `Set#to_ary` [Fix #16990] --- lib/set.rb | 13 ++++++++++++- test/test_set.rb | 5 +++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/set.rb b/lib/set.rb index cb07037..231a45d 100644 --- a/lib/set.rb +++ b/lib/set.rb @@ -191,12 +191,23 @@ def replace(enum) end # Converts the set to an array. The order of elements is uncertain. + # Sets can be converted implicitly to arrays. # # Set[1, 2].to_a #=> [1, 2] - # Set[1, 'c', :s].to_a #=> [1, "c", :s] + # Set[1, 'c', :s].to_ary #=> [1, "c", :s] + # [1, 2] + Set[1, 'c', :s] #=> [1, 2, 1, "c", :s] def to_a @hash.keys end + alias to_ary to_a + + # Converts the set to an array. The order of elements is uncertain. + # + # Set[1, 2].to_a #=> [1, 2] + # Set[1, 'c', :s].to_a #=> [1, "c", :s] + def to_ary + @hash.keys + end # Returns self if no arguments are given. Otherwise, converts the # set to another with klass.new(self, *args, &block). diff --git a/test/test_set.rb b/test/test_set.rb index 54ef80b..88eb3b7 100644 --- a/test/test_set.rb +++ b/test/test_set.rb @@ -794,6 +794,11 @@ def test_reset assert_equal(klass.new([a]), set, klass.name) } end + + def test_array_op_set + assert_equal([1,2,1,4,1], [1,2,1] + Set[4,1]) + assert_equal([1,2,4], [1,2,1] | Set[4,1]) + end end class TC_Enumerable < Test::Unit::TestCase