Ordering Your Library - 5. Sorting with Control Flow/ Code, quick question


#1

def alphabetize(arr, rev=false)
    arr.sort!
    rev ? arr.reverse! : nil
    arr
end

numbers = [3, 9, 3, 2, 1]

alphabetize(numbers)
puts numbers

After looking at other users responses prior to this lesson, I learned to add the variable arr on line four. My question is, what is the purpose of this? It did not work when I did not add arr before. Any explanation?


#2

In Ruby, the last line of a function is the return value. It's known as implicit return.


#3

Interesting. Thank you @mtf, I appreciate it.


#4

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