我不明白下面代码中的一行:
def prepare_grid
rows = Array.new(@rows)
row_height = 1.0 / @rows
rows[0] = [ PolarCell.new(0, 0) ]
(1...@rows).each do |row|
radius = row.to_f / @rows
circumference = 2 * Math::PI * radius
previous_count = rows[row - 1].length
estimated_cell_width = circumference / previous_count
ratio = (estimated_cell_width / row_height).round
cells = previous_count * ratio
rows[row] = Array.new(cells) { |col| PolarCell.new(row, col) }
end
rows
end
col如何在下面的行中获取它的值???
rows[row] = Array.new(cells) { |col| PolarCell.new(row, col) }
如何将其翻译成 Javascript?
最佳答案
col
这里是索引,可以看到如下:
arr = Array.new(5) { |i| print "#{i} "; i * 2}
# this prints the value of i each iteration:
# => 0 1 2 3 4
print arr
# each iteration returns i * 2 so the array ends up as:
# => [0,2,4,6,8]
5 是数组的长度,该 block 被调用了 5 次(由于零索引,i
传递的值从 0 到 4)。
在 Javascript 中,您可以使用 for
循环来实现,例如:
let arr = [];
for (let i = 0; i < 5; i++) {
arr.push(i);
};
关于ruby - col如何在Ruby代码中获取它的值 : Array. new(cells) { |col| PolarCell.new(行,列)},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57456889/