FlapsMack FlapsMack - 11 months ago 70
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 Source
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

puts result