Skip to content

Instantly share code, notes, and snippets.

@ahimmelstoss
Created October 11, 2013 17:12
Show Gist options
  • Save ahimmelstoss/6938488 to your computer and use it in GitHub Desktop.
Save ahimmelstoss/6938488 to your computer and use it in GitHub Desktop.
# Write a method that returns whether a given letter is a vowel, using if and elsif
def is_vowel1(letter)
letter.downcase!
if letter == "a"
return true
elsif letter == "e"
return true
elsif letter == "i"
return true
elsif letter == "o"
return true
elsif letter == "u"
return true
else
return false
end
end
# Write a method that returns whether a given letter is a vowel, using case
def is_vowel2(letter)
letter.downcase!
case letter
when "a" then true
when "e" then true
when "i" then true
when "o" then true
when "u" then true
else false
end
end
# Write a method that returns whether a given letter is a vowel, using if with no else, all on a single line
def is_vowel3(letter)
["a", "e", "i", "o", "u"].each do |vowel| return true if letter == vowel end
end
# Write a method that returns whether a given letter is a vowel without using if or case while all on a single line
def is_vowel4(letter)
letter == "a" || letter == "e" || letter == "i" || letter == "o" || letter == "u"
end
# Write a method that returns whether a given letter is a vowel without checking equality, or the use of if, using the array of vowels
def is_vowel5(letter)
vowels = ["a", "e", "i", "o", "u"]
vowels.include?(letter)
end
# Write a method that will evaluate a string and return the first vowel found in the string, if any
def first_vowel(str)
str.scan(/a|e|i|o|u/).first
end
# Write a method that will evaluate a string and return the ordinal position (index) of the string where the first vowel is found, if any
def vowel_position(str)
str.index(str.scan(/a|e|i|o|u/).first)
end
vowel_position("mdfdesfd")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment