1

Hello I am fairly new to Ruby still learning. The goal for this code is to return a boolean given a string. This method should return true if all vowels are in the string, not in a certain order but only vowels should be in string. False otherwise. Ex: str("hello") => false. str("oii") => true. I have not made any progress other than what's below.

def only_vowels(str)
   vowels = "aeiou"
    str.all? #no idea what to use
end
  • 2
    "need help writing a clean code" - make it work first, _then_ worry about making it clean. – Sergio Tulentsev May 20 '21 at 20:26
  • 1
    _"all vowels are in the string"_ and _"only vowels should be in string"_ are two different things. – Stefan May 20 '21 at 21:24
  • @HazelAlvarez : Since you received good answers to your question, may I suggest that you accept one of them, or if they still don't answer your problem, explain what's missing? – user1934428 May 21 '21 at 07:20

4 Answers4

2

You can use the .match? method for a boolean result:

def only_vowels(str)
    str.match? /^[aeiou]+$/i
end

The i makes the regex case insensitive. (You could also use (?i) in the pattern for the same effect.)

Try it out:

> only_vowels("dfg")
false
> only_vowels("aei")
true
> only_vowels("Aei")
true
> only_vowels("")
false

Note:

'Only vowels' is dependent on what is a vowel? Do we include or exclude white space? Character returns? What about vowels including diacritics such as à? What about things that sound like a vowel such as y in gym or my?

There is a great Perl post on this HERE.

dawg
  • 98,345
  • 23
  • 131
  • 206
  • `return` is spurious. There's also the `/.../i` form which is more commonly used. – tadman May 20 '21 at 22:03
  • `only_vowels("aei\nz") #=> true`. You need end-of-string, not end-of-line, anchors. – Cary Swoveland May 21 '21 at 01:25
  • 1
    @CarySwoveland: fair point. However, if a string has `\n` is it only vowels? You would need to add `\n` to to `"uooi\neea".delete('aeiou').empty?` for the same reason. (I like that method btw...) – dawg May 21 '21 at 02:04
  • @dawg : Interesting idea. The same would apply to a space, or characters such as `á` or `ä` or `あ`. Should we regard them as vowels? to me they are, but in general, the list of what exactly is a vowel, really should go only into the `[....]` part of the regexp, and not implicitly handled via the _end-of-line_ indicator of the regexp. – user1934428 May 21 '21 at 07:19
1

You can use Ruby's match method in a string for this:

def only_vowels(str)
   match = str.match(/[^aeiou]/)
   return match == nil
end

Notice that the call to match uses a regular expression that looks for anything that is not a vowel (^aeiou). If it finds nothing (match == nil) we can assume that there were only vowels.

You will need to add a check for an empty string since that will also return match == nil.

Hector Correa
  • 26,290
  • 8
  • 57
  • 73
0
"uooieea".delete('aeiou').empty?
  #=> true
"uoozieea".delete('aeiou').empty?
  #=> false

See String#delete.

Cary Swoveland
  • 106,649
  • 6
  • 63
  • 100
0

This looks like your draft, using Enumerable#all?:

str.chars.all? { |e| vowels.chars.include? e }

Of course, it is better to use a variable for the vowels array.

iGian
  • 11,023
  • 3
  • 21
  • 36