Created
September 27, 2013 13:10
-
-
Save rosiehoyem/6728383 to your computer and use it in GitHub Desktop.
Ruby homework, day 4.
This file contains hidden or 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
=begin | |
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 | |
#2 Write a method that returns whether a given letter is a vowel, | |
using case | |
#3 Write a method that returns whether a given letter is a vowel, | |
using if with no else, all on a single line | |
#4 Write a method that returns whether a given letter is a vowel | |
without using if or case while all on a single line | |
#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 | |
#6 Write a method that will evaluate a string and return the | |
first vowel found in the string, if any | |
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 | |
hint: remember that every line of ruby code has a return value, | |
and that a method will return the result of the last operation | |
=end | |
letter = "a" | |
string = "rosie" | |
vowels = ["a", "e", "i", "o", "u"] | |
#1 | |
def vowel_ifelse(letter) | |
if letter.include?("a") | |
puts "It's a vowel!" | |
elsif letter.include?("e") | |
puts "It's a vowel!" | |
elsif letter.include?("i") | |
puts "It's a vowel!" | |
elsif letter.include?("o") | |
puts "It's a vowel!" | |
elsif letter.include?("u") | |
puts "It's a vowel!" | |
else | |
puts "It's NOT a vowel!" | |
end | |
end | |
vowel_ifelse(letter) | |
#2 | |
def vowel_case(letter) | |
case(letter) | |
when "a" | |
puts "It's a vowel!" | |
when "e" | |
puts "It's a vowel!" | |
when "i" | |
puts "It's a vowel!" | |
when "o" | |
puts "It's a vowel!" | |
when "u" | |
puts "It's a vowel!" | |
else | |
puts "It's NOT a vowel!" | |
end | |
end | |
vowel_case(letter) | |
#3 | |
def vowel_if(letter) | |
puts "It's a vowel!" if "aeiou".include?(letter) | |
end | |
vowel_if(letter) | |
#4 | |
def vowel_singleline(letter) | |
("aeiou".include?(letter)) && (puts "It's a vowel!") | |
end | |
vowel_singleline(letter) | |
#5 | |
def vowel_array | |
end | |
#6 | |
def first_vowel | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment