FlapsMack FlapsMack - 3 months ago 18
Ruby Question

Search a hash for string with most vowels

So let's say I have a hash full of strings as the values. How would I make a method that will search the hash and return the string with the most vowels in it?

Answer
result = nil
max = 0

# hash is your hash with strings
hash.values.each do |value|
  vowels = value.scan(/[aeiouy]/).size

  if vowels > max
    max = vowels
    result = value
  end
end

puts result
Comments