【发布时间】:2013-04-30 07:09:56
【问题描述】:
抱歉,关于 TestFirst.org Ruby 练习编写“Pig Latin”方法的另一个问题,来自一个新手。其他答案有所帮助,但我无法成功适应它们。主要问题是我正在尝试编写一个方法来扫描一串单词(不仅仅是一个单词),修改一些单词(如果适用),然后返回完整的字符串。
下面是我尝试执行练习的第一部分的代码,即将“ay”附加到任何以元音开头的单词。但是,它对我不起作用-似乎是.include?与单个字母比较时永远不会返回 true(?)
非常感谢任何帮助!
# PIG LATIN
# If any word within the input string begins with a vowel, add an "ay" to the end of the word
def translate(string)
vowels_array = %w{a e i o u y}
consonants_array = ('a'..'z').to_a - vowels_array
string_array = string.split
string_array.each do |word|
if vowels_array.include?(word[0])
word + 'ay'
end
end
return string_array.join(" ")
end
translate("apple orange mango") # => "appleay orangeay mango" but does not
【问题讨论】:
标签: ruby