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


greggzst (Grzegorz Jakubiak) wrote:
> I see your point but Arrays are immutable

Arrays are most definitely mutable. If you freeze it, many methods will no longer work, like `[]=`, `map!`, `compact!`, etc. No method of Matrix will raise anything because a Matrix is frozen.

> It's just when you work with Matrix and you want to change one element at the time it doesn't make any sense doing it by creating arrays cause you have to create the whole Matrix again even though you've got some elements already filled in. This is how I see things.

Again, I'm not sure what is the use case. You were talking about building a Matrix from the center, I still don't know in what circumstances you'd want to do that. If you already have a matrix and want a new one with some options modified, you can:

    arrays = some_matrix.to_a
    arrays[2][2] = 42
    m = Matrix.rows(arrays, false)

Of course it's not as easy as calling []=.

greggzst (Grzegorz Jakubiak) wrote:
> marcandre (Marc-Andre Lafortune) wrote:
> > 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...
> 
> @marcandre I called it using send and it didn't change a thing in my 3x3 Matrix. So what you probably mean we'd have to add this functionality so it works as in arrays when you pass a range.

Sorry, I meant that `m[1, 1..2] = 42` was dangerous, as you no longer have a well formed matrix. The fact that `m[1..2, 1..2] = 42` does nothing is also an issue.
So yes, the method `[]=` as currently written doesn't work correctly for ranges.


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

* 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>