Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 为什么嵌套在另一个each循环中的each循环中的rand不起作用_Ruby_Each_Nested Loops - Fatal编程技术网

Ruby 为什么嵌套在另一个each循环中的each循环中的rand不起作用

Ruby 为什么嵌套在另一个each循环中的each循环中的rand不起作用,ruby,each,nested-loops,Ruby,Each,Nested Loops,考虑以下阵列和范围: friends = ["Joe", "Sam", "Tom"] ary = [rand(1..5), rand(6..10), rand(11..20)] range = (0..2) 我想创建一个代码,返回Joe,如下所示: "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 essay

考虑以下阵列和范围:

friends = ["Joe", "Sam", "Tom"]
ary = [rand(1..5), rand(6..10), rand(11..20)]
range = (0..2)
我想创建一个代码,返回Joe,如下所示:

"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"
对于萨姆和汤姆来说,每年的论文数量也不尽相同
可以使用以下代码:

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
但是,此代码是重复和冗余的,不考虑
ari
的大小可能大于此处。因此,我想使用以下更紧凑的代码:

friends.each do |friend|
  range.each do |num|
    "#{friend} at the end of year #{num+1} wrote #{ary[num]} essays"
  end
end

但是这段代码会为每个朋友返回相同数量的文章,因此使用方法
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"

您是否考虑过将范围存储在数组中,并根据需要从
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\u with\u 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的使用就没用了”-当您创建一个数组时,其中的元素-方法,它们将返回在此数组中的工作结果,下次,您可以在控制台中使用
irb

2.3.0 :001 > ary = [rand(1..5), rand(6..10), rand(11..20)]
 => [2, 9, 12]

除了@pjs之外,您还可以使用
each\u with\u 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的使用就没用了”-当您创建一个数组时,其中的元素-方法,它们将返回在此数组中的工作结果,下次,您可以在控制台中使用
irb

2.3.0 :001 > ary = [rand(1..5), rand(6..10), rand(11..20)]
 => [2, 9, 12]

each循环(或任何替代解决方案)应该在ary的每个元素上从头到尾迭代,而不重复。应遵守ary中元素的顺序,以便从第1年到第3年有一个书面文章的进度。@Asarluhi您尝试过运行提供的解决方案吗?我尊重你在问题中给出的相同进展。你是对的,我没有注意到你从ary中删除rand。非常感谢@pjs,它很有效!each循环(或任何替代解决方案)应该在ary的每个元素上从头到尾迭代,而不重复。应遵守ary中元素的顺序,以便从第1年到第3年有一个书面文章的进度。@Asarluhi您尝试过运行提供的解决方案吗?我尊重你在问题中给出的相同进展。你是对的,我没有注意到你从ary中删除rand。非常感谢@pjs,它很有效!