Created
September 27, 2013 04:15
-
-
Save rayning0/6724058 to your computer and use it in GitHub Desktop.
Vowels
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
# Write a method that returns whether a given letter is a vowel, using if and elsif | |
def is_vowel?(v) | |
if v == 'a' | |
true | |
elsif v == 'e' | |
true | |
elsif v == 'i' | |
true | |
elsif v == 'o' | |
true | |
elsif v == 'u' | |
true | |
else | |
false | |
end | |
end | |
puts "#{is_vowel?('a')}, #{is_vowel?('b')}" | |
# Write a method that returns whether a given letter is a vowel, using case | |
def is_vowel?(v) | |
case v | |
when 'a', 'e', 'i', 'o', 'u' | |
true | |
else | |
false | |
end | |
end | |
puts "#{is_vowel?('o')}, #{is_vowel?('b')}" | |
# 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?(v) | |
true if 'aeiou'.match(v) | |
end | |
puts "#{is_vowel?('a')}, #{is_vowel?('b')}" | |
# 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?(v) | |
'aeiou'.include?(v) | |
end | |
puts "#{is_vowel?('a')}, #{is_vowel?('b')}" | |
# 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?(v) | |
%w[a e i o u].include?(v) | |
end | |
puts "#{is_vowel?('a')}, #{is_vowel?('b')}" | |
# Write a method that will evaluate a string and return the first vowel found in the string, if any | |
def first_vowel(s) | |
s.match(/[aeiou]/) | |
end | |
s = 'Czech Republic' | |
puts first_vowel(s) | |
# 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 vowelposition(v) | |
v.index(/[aeiou]/) | |
end | |
puts vowelposition(s) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment