Skip to content

Instantly share code, notes, and snippets.

@constdrop
Created October 29, 2015 01:15
Show Gist options
  • Select an option

  • Save constdrop/0f71b88f91d7c9bdb6e9 to your computer and use it in GitHub Desktop.

Select an option

Save constdrop/0f71b88f91d7c9bdb6e9 to your computer and use it in GitHub Desktop.
max and min of hash's value included in array.
ary = [ { a: 1, b: 2, c: 3 }, { b: 3, c: 2 }, { a: 2, c: 4 } ]
key = :a
max = ary.select{ |h| h[key] }.map{ |h| h[key] }.max # => 2
min = ary.select{ |h| h[key] }.map{ |h| h[key] }.min # => 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment