diff --git a/spec/std/tuple_spec.cr b/spec/std/tuple_spec.cr index ec240234d8ed..31240a72fce1 100644 --- a/spec/std/tuple_spec.cr +++ b/spec/std/tuple_spec.cr @@ -333,13 +333,29 @@ describe "Tuple" do ({1, 2} === nil).should be_false end - it "does to_a" do - ary = {1, 'a', true}.to_a - ary.should eq([1, 'a', true]) - ary.size.should eq(3) + describe "#to_a" do + describe "without block" do + it "basic" do + ary = {1, 'a', true}.to_a + ary.should eq([1, 'a', true]) + ary.size.should eq(3) + end + + it "empty" do + ary = Tuple.new.to_a + ary.size.should eq(0) + end + end - ary = Tuple.new.to_a - ary.size.should eq(0) + describe "with block" do + it "basic" do + {-1, -2, -3}.to_a(&.abs).should eq [1, 2, 3] + end + + it "different type" do + {1, 2, true}.to_a(&.to_s).should eq ["1", "2", "true"] + end + end end # Tuple#to_static_array don't compile on aarch64-darwin and diff --git a/src/tuple.cr b/src/tuple.cr index 2f9cde352e4f..a658e36774c7 100644 --- a/src/tuple.cr +++ b/src/tuple.cr @@ -545,11 +545,7 @@ struct Tuple # {1, 2, 3, 4, 5}.to_a # => [1, 2, 3, 4, 5] # ``` def to_a : Array(Union(*T)) - {% if compare_versions(Crystal::VERSION, "1.1.0") < 0 %} - to_a(&.itself.as(Union(*T))) - {% else %} - to_a(&.itself) - {% end %} + to_a(&.as(Union(*T))) end # Returns an `Array` with the results of running *block* against each element of the tuple. @@ -557,8 +553,8 @@ struct Tuple # ``` # {1, 2, 3, 4, 5}).to_a { |i| i * 2 } # => [2, 4, 6, 8, 10] # ``` - def to_a(& : Union(*T) -> _) - Array(Union(*T)).build(size) do |buffer| + def to_a(& : Union(*T) -> U) forall U + Array(U).build(size) do |buffer| {% for i in 0...T.size %} buffer[{{i}}] = yield self[{{i}}] {% end %}