Skip to content

Instantly share code, notes, and snippets.

@davidbella
Created September 27, 2013 03:19
Show Gist options
  • Save davidbella/6723721 to your computer and use it in GitHub Desktop.
Save davidbella/6723721 to your computer and use it in GitHub Desktop.
Ruby: All about finding vowels
def is_vowel_elsif(letter)
if letter == "a"
true
elsif letter == "e"
true
elsif letter == "i"
true
elsif letter == "o"
true
elsif letter == "u"
true
else
false
end
end
puts is_vowel_elsif("a")
def is_vowel_case(letter)
case letter
when "a"
true
when "e"
true
when "i"
true
when "o"
true
when "u"
true
else
false
end
end
puts is_vowel_case("e")
def is_vowel_one_if(letter)
if letter == "a" || letter == "e" || letter == "i" || letter == "o" || letter == "u"
return true
end
false
end
puts is_vowel_one_if("i")
def is_vowel_no_if(letter)
"aeiou".include?(letter)
end
puts is_vowel_no_if("o")
def is_vowel_array(letter)
["a", "e", "i", "o", "u"].include?(letter)
end
puts is_vowel_array("u")
def first_vowel(string)
string.match(/a|e|i|o|u/)
end
puts first_vowel("first_vowel")
def first_vowel_index(string)
string.index(first_vowel(string).to_s)
end
puts first_vowel_index("first_vowel_index")
@gfrancomontero
Copy link

Thanks for this contribution! Adding this syntax here too :-)

def method(word)
word[0].eql?("A" || "E" || "I" || "O" || "U")
end

@thiagows2
Copy link

You can use with regex too, something like this:

def is_vowel?(string)
result = string.match(/[aeiou]/).to_s

result.present?
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment