如何实现以下目标:我想更改.each循环中管道字符之间引用的数组元素的值.
这是我想要做的一个例子,但目前没有工作:
x = %w(hello there world)
x.each { |element|
if(element == "hello") {
element = "hi" # change "hello" to "hi"
}
}
puts x # output: [hi there world]
Run Code Online (Sandbox Code Playgroud)
很难找到如此普遍的东西.
我怎样才能替换数组中的元素?
a = [1,2,3,4,5]
Run Code Online (Sandbox Code Playgroud)
我需要替换5 [11,22,33,44].flatten!
使a现在成为
a = [1,2,3,4,11,22,33,44]
Run Code Online (Sandbox Code Playgroud)