I came with below solution but I believe that must be nicer one out there ...
array = [ 'first','middle','last'] index = array.length array.length.times { index -= 1; puts array[index]}
The Ruby Enumerable#each method is the most simplistic and popular way to iterate individual items in an array. It accepts two arguments: the first being an enumerable list, and the second being a block. It takes each element in the provided list and executes the block, taking the current item as a parameter.
Ruby | Array class last() function last() is a Array class method which returns the last element of the array or the last 'n' elements from the array. The first form returns nil, If the array is empty .
In JavaScript, you'll often need to iterate through an array collection and execute a callback method for each iteration. And there's a helpful method JS devs typically use to do this: the forEach() method. The forEach() method calls a specified callback function once for every element it iterates over inside an array.
Ruby iterators return all the elements of a collection one after another.
Ruby is smart
a = [ "a", "b", "c" ] a.reverse_each {|x| print x, " " }
array.reverse.each { |x| puts x }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With