我试图遍历元音"aeiou"
并将每个字母向前移动,返回字符串"eioua"
。这是我的代码:Ruby通过字符串迭代
def vowel(letter)
vowels = "aeiou"
string = ""
index = 0
while index < letter.length
current_id = vowels.index(letter)
next_vowel = vowels[current_id + 1]
string += next_vowel
index += 1
end
string
end
当我通过"aeiou"
作为参数传递给我的方法,它只是需要"a"
,并打印"eeeee"
。
vowel("aeiou") # => "eeeee"
你的问题是什么? – sawa