How to print an array of elements in reverse order not just single digit number but also multi-digit numbers.
[2, 5, 6 7]
It should print the array elements in reverse order as 7 6 5 2 by following a space for each number.
I already wrote the code for this.
puts "Enter the array elements"
arr = gets.strip
arr = arr.split(' ').map(&:to_i)
x = arr.reverse_each {|f| }
z = x.join(" ")
print z.reverse
That is cool with single digit numbers, how can I reverse the multi-digit numbers in an array of inputs given by the user input like:
[45, 76, 87 ] # this should reverse the array as `87 76 45`
[556, 674, 878 ] # this should reverse the array as `878 674 556`
[8797, 7347, 9374 ] # this should reverse the array as `9374 7374 8797`
n? It is never used in the above code.nis just used to know many elements does the array contain. However, I didn't usenin the above code. Maybe I should remove that code.!reversemethod,ArrayandString. You are using the latter one.puts gets.strip.split(' ').map(&:to_i).reverse.inspect[34, 54,76] => [67, 45, 43]' with bothreverse` methodsreverse_eachandreversewhich is not I want.