Created
September 27, 2013 02:56
-
-
Save TrevMcKendrick/6723596 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Let's go back to the exercise where we determined what is and isn't a vowel. | |
# With ruby, there's always more than one way to do something and get the same | |
# result. | |
# Assuming vowels `a,e,i,o,u`: | |
# 1. Write a method that returns whether a given letter is a vowel, using `if` | |
# and `elsif` | |
def is_vowel_one?(letter) | |
letter.downcase! | |
if letter == "a" | |
true | |
elsif letter == "e" | |
true | |
elsif letter == "i" | |
true | |
elsif letter == "o" | |
true | |
elsif letter == "u" | |
true | |
else | |
false | |
end | |
end | |
# 2. Write a method that returns whether a given letter is a vowel, | |
# using `case` | |
def is_vowel_two?(letter) | |
letter.downcase! | |
case letter | |
when "a" | |
true | |
when "e" | |
true | |
when "i" | |
true | |
when "o" | |
true | |
when "u" | |
true | |
else | |
false | |
end | |
end | |
# 3. Write a method that returns whether a given letter is a vowel, | |
# using `if` with no `else`, all on a single line | |
def is_vowel_three?(letter) | |
["a","e","i","o","u"].each { |vowel| return true if vowel == letter } | |
false | |
end | |
# 4. 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_vowel_three?(letter) | |
["a","e","i","o","u"].include?(letter.downcase) | |
end | |
# 5. 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_vowel_four?(letter) | |
["a","e","i","o","u"].include?(letter.downcase) | |
end | |
# 6. Write a method that will evaluate a string and return the first vowel found in | |
# the string, if any | |
def first_vowel(word) | |
letter_array = word.each_char.to_a | |
for i in 0..(word.length - 1) do | |
return letter_array[i] if ["a","e","i","o","u"].include?(letter_array[i].downcase) | |
end | |
end | |
# 7. 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 first_vowel_two(word) | |
letter_array = word.each_char.to_a | |
for i in 0..(word.length - 1) do | |
return [i] if ["a","e","i","o","u"].include?(letter_array[i].downcase) | |
end | |
"No vowel found" | |
end | |
# *hint: remember that every line of ruby code has a return value, and that a | |
# *method will return the result of the last operation | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment