为什么嵌套在另一个 each 循环中的 each 循环中的 rand 不起作用
Why rand inside an each loop nested in another each loop does not work
考虑以下数组和范围:
friends = ["Joe", "Sam", "Tom"]
ary = [rand(1..5), rand(6..10), rand(11..20)]
range = (0..2)
我想为 Joe 创建一个 return 代码,如下所示:
"Joe at the end of year 1 wrote 2 essays"
"Joe at the end of year 2 wrote 8 essays"
"Joe at the end of year 3 wrote 16 essays"
Sam 和 Tom 每年的论文数量不同。
可以使用以下代码:
friends.each do |friend|
"#{friend} at the end of year 1 wrote #{rand(1..5)} essays"
"#{friend} at the end of year 2 wrote #{rand(6..10)} essays"
"#{friend} at the end of year 3 wrote #{rand(11..20)} essays"
end
然而这段代码是重复和冗余的,没有考虑到 ary
的大小可以比这里更大。所以我想使用以下更紧凑的代码:
friends.each do |friend|
range.each do |num|
"#{friend} at the end of year #{num+1} wrote #{ary[num]} essays"
end
end
但是这段代码会给每个朋友return相同数量的文章,所以使用方法rand
就没用了。这是为什么?您会建议什么解决方案?
您是否考虑过将范围存储在数组中,并根据需要从 rand
中绘制?
friends = ["Joe", "Sam", "Tom"]
ary =[(1..5), (6..10), (11..20)]
year = (1..3)
friends.each do |friend|
year.each do |yr|
p "#{friend} at the end of year #{yr} wrote #{rand(ary[yr - 1])} essays"
end
end
这会产生,例如:
"Joe at the end of year 1 wrote 5 essays"
"Joe at the end of year 2 wrote 7 essays"
"Joe at the end of year 3 wrote 16 essays"
"Sam at the end of year 1 wrote 3 essays"
"Sam at the end of year 2 wrote 7 essays"
"Sam at the end of year 3 wrote 18 essays"
"Tom at the end of year 1 wrote 2 essays"
"Tom at the end of year 2 wrote 8 essays"
"Tom at the end of year 3 wrote 15 essays"
除了@pjs,还可以使用each_with_index
方法
friends = ["Joe", "Sam", "Tom"]
ary =[(1..5), (6..10), (11..20)]
friends.each do |friend|
ary.each_with_index do |value, year|
p "#{friend} at the end of year #{year+1} wrote #{rand(value)} essays"
end
end
另外,回答你的问题:“.. 这样使用方法 rand 就没用了” - 当你创建一个数组时,其中的元素 - 方法,它们将 return 结果他们在这个数组中的工作,下次,你可以在你的控制台中尝试,使用 irb
:
2.3.0 :001 > ary = [rand(1..5), rand(6..10), rand(11..20)]
=> [2, 9, 12]
考虑以下数组和范围:
friends = ["Joe", "Sam", "Tom"]
ary = [rand(1..5), rand(6..10), rand(11..20)]
range = (0..2)
我想为 Joe 创建一个 return 代码,如下所示:
"Joe at the end of year 1 wrote 2 essays"
"Joe at the end of year 2 wrote 8 essays"
"Joe at the end of year 3 wrote 16 essays"
Sam 和 Tom 每年的论文数量不同。
可以使用以下代码:
friends.each do |friend|
"#{friend} at the end of year 1 wrote #{rand(1..5)} essays"
"#{friend} at the end of year 2 wrote #{rand(6..10)} essays"
"#{friend} at the end of year 3 wrote #{rand(11..20)} essays"
end
然而这段代码是重复和冗余的,没有考虑到 ary
的大小可以比这里更大。所以我想使用以下更紧凑的代码:
friends.each do |friend|
range.each do |num|
"#{friend} at the end of year #{num+1} wrote #{ary[num]} essays"
end
end
但是这段代码会给每个朋友return相同数量的文章,所以使用方法rand
就没用了。这是为什么?您会建议什么解决方案?
您是否考虑过将范围存储在数组中,并根据需要从 rand
中绘制?
friends = ["Joe", "Sam", "Tom"]
ary =[(1..5), (6..10), (11..20)]
year = (1..3)
friends.each do |friend|
year.each do |yr|
p "#{friend} at the end of year #{yr} wrote #{rand(ary[yr - 1])} essays"
end
end
这会产生,例如:
"Joe at the end of year 1 wrote 5 essays"
"Joe at the end of year 2 wrote 7 essays"
"Joe at the end of year 3 wrote 16 essays"
"Sam at the end of year 1 wrote 3 essays"
"Sam at the end of year 2 wrote 7 essays"
"Sam at the end of year 3 wrote 18 essays"
"Tom at the end of year 1 wrote 2 essays"
"Tom at the end of year 2 wrote 8 essays"
"Tom at the end of year 3 wrote 15 essays"
除了@pjs,还可以使用each_with_index
方法
friends = ["Joe", "Sam", "Tom"]
ary =[(1..5), (6..10), (11..20)]
friends.each do |friend|
ary.each_with_index do |value, year|
p "#{friend} at the end of year #{year+1} wrote #{rand(value)} essays"
end
end
另外,回答你的问题:“.. 这样使用方法 rand 就没用了” - 当你创建一个数组时,其中的元素 - 方法,它们将 return 结果他们在这个数组中的工作,下次,你可以在你的控制台中尝试,使用 irb
:
2.3.0 :001 > ary = [rand(1..5), rand(6..10), rand(11..20)]
=> [2, 9, 12]