申し訳ありませんが、「Pig Latin」メソッドを作成するための TestFirst.org Ruby 演習について、初心者からの別の質問です。他の回答は多少役に立ちましたが、うまく適応させることができませんでした。主な問題は、単語の文字列 (単一の単語だけでなく) をスキャンし、単語の一部を変更し (該当する場合)、完全な文字列を返すメソッドを作成しようとしていることです。
以下は、演習の最初の部分を実行しようとする私のコードです。これは、母音で始まる単語に「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