Search code examples
arraysrubyblock

Return an array of vowels used within a string


I am struggling with a ruby challenge the task is

Write a method that will take a string and return an array of vowels used in that string.

Example: count_vowels("The quick brown fox") should return ["e","u","i","o","o"]

count_vowels("Hello World") should return ["e","o","o"]

so far i have tried experimenting with blocks and other array methods like

def vowels(string)
 string_array = string.chars
  vowels = ["a", "e", "i", "o", "u"]
  p string_array & vowels

end

also

def vowels (string)
  # Your code here
  arr =(string).downcase.chars
    new =[]
  values = ["a","e","i","o","u"]
arr. { |words| values.include?(words.each) }
    end

Solution

  • Below code piece should help you

    Edited:-

    def count_vowels(word)
       word.downcase.chars.select{|c|  %[a,e,i,o,u].include?(c)}
    end