How to find a min/max with Ruby

后端 未结 6 2212
长情又很酷
长情又很酷 2020-12-04 05:29

I want to use min(5,10), or Math.max(4,7). Are there functions to this effect in Ruby?

6条回答
  •  借酒劲吻你
    2020-12-04 05:55

    If you need to find the max/min of a hash, you can use #max_by or #min_by

    people = {'joe' => 21, 'bill' => 35, 'sally' => 24}
    
    people.min_by { |name, age| age } #=> ["joe", 21]
    people.max_by { |name, age| age } #=> ["bill", 35]
    

提交回复
热议问题