array = [apple, orange]
number = 4
Run Code Online (Sandbox Code Playgroud)
所需的输出:
apple
orange
apple
orange
Run Code Online (Sandbox Code Playgroud)
到目前为止,我有:
array.each do |x|
puts x
end
Run Code Online (Sandbox Code Playgroud)
我只是不确定如何遍历数组 4 次。
数组 = [13845, 23424, 89021, 83628]
我将如何返回每个元素的最后 2 个数字?
预期产出:45 24 21 28
我尝试过 array[i].to_s.last(2) 但我收到了错误消息:“未定义方法 `last' for "13845":String”
如何将多个数组合并到一个循环中?我希望他们一次只迭代一个。
例子:
a = [1, 2, 3, 4]
b = [a, b, c, d]
c = [w, x, y, z]
Run Code Online (Sandbox Code Playgroud)
所需的输出:
“在1日当天,有一个1上一个添加w ^。”
“第2天,在b上有一个2。加上x。”
“第3天,c上出现了3。添加y。”
“第4天,d上有一个4。加上z。”
另外,我们可以合并 .each_with_index 吗?因为我还需要在循环中打印索引号。“在第 1 天/第 2 天/等天”中的数字是我输入 #{index + 1} 的地方。