This is my array and custom method to reverse an array output without using the reverse method. not sure where it broke, tried running it in console, no dice.
numbers = [1, 2, 3, 4, 5, 6]
def reversal(array)
do |item1, item2| item2 <=> item1
end
p reversal(numbers)
<=>does?[1,3,2], is the correct output[3,2,1]or[2,3,1]?doand thedo |stuff|construct are part of a method call, not a method definition. Of course, you can call a method from within the body of another method, but you're not doing that. Also, everydoneeds a correspondingendregardless.[1,2,3,4,5,6]is already in order, so putting it backwards from what it is now and backwards from 'in order' is the same thing. What if you had a list that was[1,2,4,5,6,3]? Would you want the result to be[6,5,4,3,2,1]or[3,6,5,4,2,1]?defand ends withend, whereas a method invocation is where you call the method. Fromdef reversal(array)toendis a method definition (though not a valid one). The last line is a method call (actually two method calls, one call toreversaland one call top). Method calls can include a 'block' (though many don't), which is a bunch of code betweendoandendor between{and}. Like in @roychri's answer below, he passes a block to the methodloop