Created
August 6, 2013 23:47
-
-
Save bahrieinn/6169941 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
# Solution for Challenge: Pig-latin. Started 2013-08-06T15:39:01+00:00 | |
def is_vowel?(word) #add is_vowel? helper method to make code in | |
/[aeiou]/i === word[0] #to_pig_latin more expressive | |
end | |
def to_pig_latin(word) | |
if is_vowel?(word) | |
word | |
else | |
word.gsub(/([^aeiou])(.*)/, '\2\1ay') #use gsub to construct new string | |
end | |
end | |
def pig_latin_word | |
puts "Please enter a word:" | |
word = gets.chomp | |
to_pig_latin(word) | |
end | |
def pig_latin_sentence | |
puts "Please enter a sentence:" | |
sentence = gets.chomp | |
words = sentence.split(" ") | |
words.map! { |word| to_pig_latin(word) } #collapse do/end block to single line | |
"Your sentence has #{words.count} words: #{words.join(" ")}" | |
end | |
# driver code | |
# puts is_vowel?("Airplanes") == true | |
# puts is_vowel?("n0 v0w3ls") == false | |
p pig_latin_word | |
p pig_latin_sentence |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment