LoginSignup
19
18

More than 5 years have passed since last update.

Hash#maxでkeyではなくvalueで比較する

Posted at
# 単にmaxとするとキーで比較される
h = {a: 29, b: 23, c: 39, d: 3}
# x[0]とするとキーを参照できる
p h.max{ |x, y| x[1] <=> y[1] } 
# => [:c, 39]
19
18
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
18