Return arr the same as arr?



My code works, but I saw on the forum that people use "else return arr" instead of "else arr". Is there a difference or can you use either? Is one better than the other?

Thanks !

def alphabetize(arr, rev=false)
    if rev
numbers = [7, 3, 5, 1, 9, 2, 3, 4, 6, 8]
puts alphabetize(numbers)


Yes, when it is the last line of a block the return is implicit.

We cannot flat out say one is better. Each situation is different. Given the many different ways that we can do the same thing, it is probably best to not think qualitatively until you have a few miles of code under your belt. Then YOU get to decide which is better in the given circumstances. Limit the amount of subjective thinking and be as objective as possible.


Thank you, that's helpful!


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.