When trying to get an upper or lower value for a certain field in an object can be cumbersome. But when using the max_by
method, this can actually be done quite elegantly. For example:
persons = [
{ "name" => "John McJohnson","age" => 34, "topScoreAtBowling" => 205},
{ "name" => "Davey Jones","age" => 304, "topScoreAtBowling" => 300},
{ "name" => "Willy Wonka","age" => 50, "topScoreAtBowling" => 200}
]
If we had a bowling competition and wanted to see who had the topScoreAtBowling
field, rather than looping through the array and looking at each object, we can simply use the max_by
command as follows:
persons.max_by { |person| person['topScoreAtBowling']} #{"name"=>"Davey Jones", "age"=>304, "topScoreAtBowling"=>300}
The resultant object would be returned, and could be processed into their “Hall of Fame”!
Conclusion
The max_by
method is very convenient when dealing with a large array of objects, and you are required to find the highest value for a certain field. Alternatively the min_by
method can be used to find the lower value of a certain field