ruby - col如何在Ruby代码中获取它的值 : Array. new(cells) { |col| PolarCell.new(行,列)}

标签 ruby

我不明白下面代码中的一行:

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/

相关文章:

ruby-on-rails - 如何正确地将 .rb 文件中的代码包含到 Rails 架构中?

ruby-on-rails - 如何使特定 View 不继承自 application.html.erb?

ruby - Mongoid 自定义 setter/getter 和 super

ruby - 无法在带有 RVM : Error running make 的 Mountain Lion 上安装 Ruby 1.9.3

arrays - 如何匹配两个数组的内容并得到对应的索引ruby

ruby-on-rails - VB .NET 中有 Rails partials 的类似物吗?

ruby-on-rails - Ruby 2.1.0/2.1.1/2.1.2 不支持调试器 gem

ruby - 将 xpath 与 Firewatir 结合使用

ruby-on-rails - Rails - POST 方法的路由错误

ruby - Ruby 1.9.2 中 `File.read` 中的可选参数