I have a coding challenge to reverse a an array with 5 elements in it. How would I do this without using the reverse method?
Code:
def reverse(array) a
One thought :-
ary = ["a", 1, "apple", 8, 90] ary.values_at(*(ary.size-1).downto(0)) # => [90, 8, "apple", 1, "a"]
ary.size.downto(0) gives #. And *# is just a Enumerable#to_a method call which splats the Enumerator to [4, 3, 2, 1, 0]. Finally, Array#values_at is working as documented.
ary.size.downto(0)
#
*#
Enumerator
[4, 3, 2, 1, 0]
Array#values_at