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.
我有这个数组:
array = ["the","quick","brown","fox"] array[0,2] = "substitute" puts array
我以为我会得到:“替代快速替代狐狸”
但相反,我得到了“替代棕狐”
我可以说这是一个菜鸟错误,我以为我可以通过编写array [0,2] = x来替换数组中的多个对象,但似乎不是,那么,如何替换数组中的多个对象在同一行?
[0,2].each { |i| array[i] = "substitute" }
您可以为此使用范围。
(0...array.length).step(2).each { |i| array[i] = "substitute" }