Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify Enumerable#to_a #15432

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/enumerable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/hash.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 0 additions & 11 deletions src/indexable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#
# ```
Expand Down
2 changes: 1 addition & 1 deletion src/tuple.cr
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ struct Tuple
# {1, 2, 3, 4, 5}.to_a # => [1, 2, 3, 4, 5]
# ```
def to_a : Array(Union(*T))
to_a(&.as(Union(*T)))
super
end

# Returns an `Array` with the results of running *block* against each element of the tuple.
Expand Down