How to find a hash key containing a matching value

后端 未结 10 754
走了就别回头了
走了就别回头了 2020-12-04 05:24

Given I have the below clients hash, is there a quick ruby way (without having to write a multi-line script) to obtain the key given I want to match the cli

10条回答
  •  没有蜡笔的小新
    2020-12-04 05:46

    The best way to find the key for a particular value is to use key method that is available for a hash....

    gender = {"MALE" => 1, "FEMALE" => 2}
    gender.key(1) #=> MALE
    

    I hope it solves your problem...

提交回复
热议问题