SG17 SG17 - 1 month ago 12
Ruby Question

Writting a method to check the number of vowels in a string

I have a hash of data that is holding different strings as its Key. I need to create a new method in my class that will count the number of vowels in each key and then return the key with the most vowels. I am very stuck and this is what I have so far.

def favorite_wish
vowels = ["a", "e", "i", "o", "u"]
@submitted_wishes.each_key do |wish|
wish.split(' ')
wish.each do |check|
if check == vowels
end
end
end


Can anyone help?

Answer

String#count might help you:

def favorite_wish
  max_key = @submitted_wishes.keys.max_by { |wish| wish.count('aeiou') }
  @submitted_wishes[max_key]
end
Comments