我希望重复一个字的可选次数。但是,当我运行我的程序时,它似乎没有超过input[1].times do line
。Ruby .join方法返回原始数组的长度而不是连接字符串的长度
CODE:
def repeat(*input)
sentence = []
if input.length == 1
"#{input[0]} #{input[0]}"
else
input[1].times do
sentence.push(input[0])
sentence.join(" ")
end
end
end
puts repeat("Hey!")
puts repeat("Hey", 3)
OUTPUT:
Hey! Hey!
3
啊!是的,非常感谢你,即使只是将“.join”方法从'.times'循环中移出来也行,因为它应该返回最后一行。再次感谢! – scabbyjoe 2014-10-18 21:03:48