Created
September 27, 2013 03:41
-
-
Save samtalks/6723823 to your computer and use it in GitHub Desktop.
9/26 HW: Different ways to...
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: | |
# Write a method that returns whether a given letter is a vowel, using if and elsif | |
def is_vow(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 | |
# Write a method that returns whether a given letter is a vowel, using case | |
def is_vow2(letter) | |
case letter | |
when "a", "e", "i", "o", "u" | |
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_vow3(letter) | |
true if letter.match(/[aeiou]/) | |
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_vow4(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 | |
# DOESN'T WORK: | |
def is_vow5(letter) | |
["a", "e", "i", "o", "u"].select do |vowel| | |
vowel == letter | |
end | |
end | |
# Write a method that will evaluate a string and return the first vowel found in the string, if any | |
def first_vow6(str) | |
puts str[str.index(/[aeiou]/)] if str.index(/[aeiou]/) != nil | |
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 | |
# DON'T KNOW WHAT AN ORDINAL POSITION IS | |
def first_vow7(str) | |
str.index(/[aeiou]/).ord | |
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