Skip to content

Instantly share code, notes, and snippets.

@natemccurdy
Last active April 18, 2017 22:17
Show Gist options
  • Save natemccurdy/a6b50deb2ebb224aa568197e5bde56d9 to your computer and use it in GitHub Desktop.
Save natemccurdy/a6b50deb2ebb224aa568197e5bde56d9 to your computer and use it in GitHub Desktop.
Find the name of a key whose attribute matches some string
$service_accounts = {
'larry' => {
gid => '1111',
tag => 'group_a',
},
'moe' => {
gid => '2222',
tag => 'group_b',
},
'curly' => {
gid => '3333',
tag => 'group_c',
},
}
$group_b_svc_account = $service_accounts.filter |$name, $attr| {
$attr['tag'] == 'group_b'
}.keys[0]
notice($group_b_svc_account)
@natemccurdy
Copy link
Author

natemccurdy commented Apr 18, 2017

This also works, but is arguably harder to read...

$group_b_svc_account = $service_accounts.reduce |$memo, $user_array| {
  ($user_array[1]['tag'] == 'group_b') ? {
    true  => $user_array[0],
    false => $memo
  }
}

notice($group_b_svc_account)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment