Issue #14151 has been updated by marcandre (Marc-Andre Lafortune).

Status changed from Open to Feedback

greggzst (Grzegorz Jakubiak) wrote:
> I don't even understand why this method hasn't been public since the beginning.

The library was originally written such that Matrices are immutable. I can't speak to the exact reason for this choice as I'm not the original author, but I suspect it was to go along with the all the other numeric objects being immutable. Makes it more natural to do functional programming too.

The method []= as it is written is also dangerous as it doesn't do any type checking. Try calling `m[1..2, 1..2] = 42` for example...

> from the center of the matrix.

I wonder what is that usecase.

> So what I wanted to do is to create a zero matrix and the fill it in a proper way but I can't without using #[]=. 

You can build the values from arrays and call Matrix.[]:

    values = Array.new(5) { Array.new(5, 0) }
    values[2][2] = 42
    m = Matrix[values]

> 


shevegen (Robert A. Heiler) wrote:
> Matrix seems a bit weird ... it does not allow for a Matrix.new either

Indeed. As documented in the source, the official constructors are `Matrix.[]`, `rows`, `columns`, `build`, ...
Note that the private `Matrix.new` makes no check on the validity of the given arguments.

> The documentation is also lacking

This is not a very constructive comment, nor is it particularly relevant to this issue. Better would be to open a separate issue, in which you enumerate which methods you fell lack documentation. Even better would be to provide a PR with the missing documentation.



----------------------------------------
Feature #14151: Make Matrix#[]= public method
https://bugs.ruby-lang.org/issues/14151#change-68256

* Author: greggzst (Grzegorz Jakubiak)
* Status: Feedback
* Priority: Normal
* Assignee: marcandre (Marc-Andre Lafortune)
* Target version: 
----------------------------------------
I don't even understand why this method hasn't been public since the beginning. I've come to a point when I have to create a matrix in a specific way using row and column indices and I can't use #build with a block because then indices go from the beginning of matrix whereas I have to from the center of the matrix. So what I wanted to do is to create a zero matrix and the fill it in a proper way but I can't without using #[]=. I know I can reopen class and that's what I'm doing but this just doesn't make sense. If we can change elements in an array like so using #[]= then why matrices can't use that as well?



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>