I am trying to write a regex that takes a word and returns true for words starting with a vowel and returns false for words starting with a consonant. I have never written regex before, and I'm a little confused on how to write the expression. This is what I have so far:
def starts_with_a_vowel?(word)
if word.match(/\A+[aeiou]/) == true
return true
else
return false
end
end
Edit: So if word = "boat" , expression should return false. If word = "apple", expression should return true.
EDIT:
OK.. So.. I never tested the code I formerly wrote.. it was meant only as some suggestion how to use the
match
with regex, but it not worked at all.. (that code snippet is now attached to the end of my answer fyi)this here should be the working one:
..but how it was mentioned by Eric Duminil here below in comments, the method/function is not needed
!!word.capitalize.match(/\A+[AEIOU]/)
can be used directly.. it returnstrue
orfalse
but there are surely other (maybe better) solutions too..
..and here is the NOT working code, which I formerly wrote:
..the
match
method returnsnil
when not match and becausenil
has nolength
method defined, it raisesNoMethodError