*在范围开始时是什么意思?

阿图尔·莫罗辛斯基(Artur Mrozinski)

很抱歉这个菜鸟问题,但是这个星号在范围开始时是什么意思?

class Matrix
  def initialize(matrix_string)
    @matrix = matrix_string.split("\n").map do |row|
        row.split.map(&:to_i)
    end
    @rows = rows.length
    @cols = columns.length
  end

  def rows
    @matrix
  end

  def columns
    @matrix.transpose
  end

  # --->>***
  def saddle_points
    [*0...@rows].product([*0...@cols]).select do |coords|
        saddle_point?(*coords)
  end
  # ***<----
end

private
  def   saddle_point?(row, col)
    (@matrix[row][col] == rows[row].max) && 
    (@matrix[row][col] == columns[col].min)
  end
end
阿列克谢·马蒂什金(Alexei Matiushkin)

文档中所述

您可以Array使用*(或splat)运算符变成参数列表

arguments = [1, 2, 3]
my_method(*arguments)

可能会对以下内容执行相同的操作Range

arguments = 1..3
my_method(*arguments) # essentially the same as my_method(1, 2, 3)

此外,在数组声明内的范围隐式转换Range之前,允许使用splat运算符Array

[*1..3]
#⇒ [1, 2, 3]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章