From 446f399768ab09c943878dc35a74f2cc458c59d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= Date: Fri, 7 Feb 2025 19:25:04 +0100 Subject: [PATCH 1/3] Simplify `Enumerable#to_a` --- src/enumerable.cr | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/enumerable.cr b/src/enumerable.cr index 0993f38bbc4d..9fcba66ddf3a 100644 --- a/src/enumerable.cr +++ b/src/enumerable.cr @@ -2013,9 +2013,7 @@ module Enumerable(T) # (1..5).to_a # => [1, 2, 3, 4, 5] # ``` def to_a : Array(T) - ary = [] of T - each { |e| ary << e } - ary + to_a(&.as(T)) end # Returns an `Array` with the results of running *block* against each element of the collection. From 6d88791ecf8ab293635c7c648f9e2e0d5c5ed4ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= Date: Fri, 7 Feb 2025 19:34:01 +0100 Subject: [PATCH 2/3] Drop redundant `Indexable#to_a` --- src/indexable.cr | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/indexable.cr b/src/indexable.cr index 4a3990e83870..3f6dca1762b1 100644 --- a/src/indexable.cr +++ b/src/indexable.cr @@ -693,17 +693,6 @@ module Indexable(T) end end - # Returns an `Array` with all the elements in the collection. - # - # ``` - # {1, 2, 3}.to_a # => [1, 2, 3] - # ``` - def to_a : Array(T) - ary = Array(T).new(size) - each { |e| ary << e } - ary - end - # Returns an `Array` with the results of running *block* against each element of the collection. # # ``` From 52ed15eb8bbd8ef40d965d30a6773345c5faf820 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= Date: Fri, 7 Feb 2025 19:41:22 +0100 Subject: [PATCH 3/3] Remove redundant implementations and delegate to `super` instead --- src/hash.cr | 2 +- src/tuple.cr | 6 +----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/hash.cr b/src/hash.cr index 1be6543d730c..c145bda36309 100644 --- a/src/hash.cr +++ b/src/hash.cr @@ -2083,7 +2083,7 @@ class Hash(K, V) # # The order of the array follows the order the keys were inserted in the Hash. def to_a : Array({K, V}) - to_a(&.itself) + super end # Returns an `Array` with the results of running *block* against tuples with key and values diff --git a/src/tuple.cr b/src/tuple.cr index 2f9cde352e4f..9078a278f4fe 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 %} + super end # Returns an `Array` with the results of running *block* against each element of the tuple.