【发布时间】:2015-03-31 04:32:27
【问题描述】:
我是编程新手,我正在使用 Ruby 作为我的入门语言。下面的代码有效,但如果有人输入了多个单词,则 pigatize 方法仅适用于第一个单词,并将额外的 ay 或 way 添加到最后一个单词。我如何让它应用于用户输入的每个单词?
# If the first letter is a vowel, add "way" to the end
# If the first letter is a consonant, move it to the end and add "ay"
class PigLatin
VOWELS = %w(a e i o u)
def self.pigatize(text)
if PigLatin.vowel(text[0])
pigalatin = text + 'way'
else
piglatin = text[1..-1] + text[0] + 'ay'
end
end
def self.vowel(first_letter)
VOWELS.include?(first_letter)
end
end
puts 'Please enter a word and I will translate it into Pig Latin. Ippyyay!.'
text = gets.chomp
puts "Pigatized: #{PigLatin.pigatize(text)}"
【问题讨论】: