Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
出乎我意料的是,输出中的数组具有 ["89"] 值,而不是预期的 ["123456","89"]。为什么会这样?
string = "" array = [] 10.times { |i| if i != 7 string << "#{i}" else array << string string.clear end }
如果你这样做
string.clear
您只是在更改引用的字符串,这就是为什么0123456您最初推入数组的内容突然变成了89之后的原因。
0123456
89
你可能想要
string = ""