Ruby: search for value in hash

August 16, 2022  ‐ 1 min read

Normally you probably just use keys to access values in a hash. But in some cases you might just be interested whether a hash contains a certain value, not per se at a given key.

For this use-case we have the .value? method, which returns true when a value is found in a hash and false otherwise.

hash = {
  python: 'Django',
  php: 'Laravel',
  ruby: 'Ruby on Rails',

puts hash.value?('Django')
#=> true

puts hash.value?('Express')
#=> false

Do keep in mind here that this method is case sensitive if you are searching for string values.