相关疑难解决方法(0)

更改.each循环中引用的数组元素的值?

如何实现以下目标:我想更改.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)

很难找到如此普遍的东西.

ruby arrays

37
推荐指数
3
解决办法
6万
查看次数

如何替换数组的元素?

我怎样才能替换数组中的元素?

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)

ruby arrays

26
推荐指数
6
解决办法
5万
查看次数

标签 统计

arrays ×2

ruby ×2