Skip to content

Instantly share code, notes, and snippets.

@samtalks
Created September 27, 2013 03:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save samtalks/6723823 to your computer and use it in GitHub Desktop.
Save samtalks/6723823 to your computer and use it in GitHub Desktop.
9/26 HW: Different ways to...
# 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