method

max

v1_8_6_287 - Show latest stable - Class: Enumerable
max()
public

Returns the object in enum with the maximum value. The first form assumes all objects implement Comparable; the second uses the block to return a <=> b.

   a = %w(albatross dog horse)
   a.max                                  #=> "horse"
   a.max {|a,b| a.length <=> b.length }   #=> "albatross"

2Notes

Capping values

Mange · Feb 16, 20092 thanks

This method is very useful when you want to cap values:

minimum ≤ value

value = [input.to_i, minimum].max

value ≤ maximum

value = [input.to_i, maximum].min

minimum ≤ value ≤ maximum

value = [ [input.to_i, minimum].max, maximum ].min

Practical example: Make sure destination is within container

destination.x = [ [current.x + current.velocity.x, 0].max, container.width ].min destination.y = [ [current.y + current.velocity.y, 0].max, container.height ].min

Video Explanation for max and min

MattStopa · Mar 14, 2012