我意识到我的沟通技巧并不那么出色(我甚至在试图查找它时遇到问题),所以我让代码说话:
easy_as_123 = ("a".."c").to_a
10.times do |j|
if j >= easy_as_123.length
puts "j is #{j}, letter is #{easy_as_123[j % easy_as_123.length]}"
else
puts "j is #{j}, letter is #{easy_as_123[j]}"
end
end
Run Code Online (Sandbox Code Playgroud)
是否有一个更优雅和简洁的解决方案,以不断迭代我的[a,b,c]数组?
编辑,我发布的代码作品,这正是我正在寻找的结果,但它不简洁也不好看,有没有方法能够以更优雅的方式实现同样的结果?
我无法理解这种红宝石行为,代码更好地解释了我的意思:
class DoNotUnderstand
def initialize
@tiny_array = [3,4]
test
end
def messing(ary)
return [ary[0]+=700, ary[1]+=999]
end
def test
puts @tiny_array.join(",") # before => 3,4
messing(@tiny_array)
puts @tiny_array.join(",") # after => 703,1003
end
end
question = DoNotUnderstand.new
Run Code Online (Sandbox Code Playgroud)
@tiny_array是的[3,4],[703,1003]
如果我不使用课程,那就会发生:
@tiny = [1,2]
def messing(ary)
return [ary[0]+693,ary[1]+999]
end
puts @tiny.join(",") # before => 1,2
messing(@tiny)
puts @tiny.join(",") # after => 1,2
Run Code Online (Sandbox Code Playgroud)
数组只是[1,2]
为什么?