Skip to content

Instantly share code, notes, and snippets.

@scottdomes
Forked from davidvandusen/max.rb
Last active March 1, 2016 19:35
Show Gist options
  • Save scottdomes/6a4fb44be19d19e35478 to your computer and use it in GitHub Desktop.
Save scottdomes/6a4fb44be19d19e35478 to your computer and use it in GitHub Desktop.
# Find the maximum
def maximum(arr)
arr.inject { |x, max| x > max ? x : max }
end
# expect it to return 42 below
result = maximum([2, 42, 22, 02])
puts "max of 2, 42, 22, 02 is: #{result}"
# expect it to return nil when empty array is passed in
result = maximum([])
puts "max on empty set is: #{result.inspect}"
result = maximum([-23, 0, -3])
puts "max of -23, 0, -3 is: #{result}"
result = maximum([6])
puts "max of just 6 is: #{result}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment