Feature #2771: Matrix: constructor to build with block
http://redmine.ruby-lang.org/issues/show/2771

Author: Marc-Andre Lafortune
Status: Open, Priority: Normal
Assigned to: Keiju Ishitsuka, Category: lib, Target version: 1.9.2

I believe the following simple constructor could be a helpful addition to matrix.rb

class Matrix
  #
  # Creates a matrix of +row_size+ x +column_size+.
  # It fills the values by calling the given block,
  # passing the current row and column.
  #
  #   m = Matrix.build(2, 4) {|row, col| col - row }
  #     => Matrix[[0, 1, 2, 3], [-1, 0, 1, 2]]
  #   m = Matrix.build(3) { rand }
  #     => a 3x3 matrix with random elements
  #
  def self.build(row_size, column_size = row_size)
    raise ArgumentError if row_size < 0 || column_size < 0
    return to_enum :build, row_size, column_size unless block_given?
    rows = row_size.times.map do |i|
      column_size.times.map do |j|
        yield i, j
      end
    end
    new rows, column_size
  end
end


----------------------------------------
http://redmine.ruby-lang.org