# Matrix modifying and coercion question?

**URL:** <https://www.ruby-forum.com/t/matrix-modifying-and-coercion-question/67997>\
**Category:** Ruby\
**Created:** [September 8, 2006, 11:47am UTC](https://www.ruby-forum.com/t/matrix-modifying-and-coercion-question/67997 "2006-09-08T11:47:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![surendra\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@surendra\_s](https://www.ruby-forum.com/u/surendra_s)\
**Post date:** [September 8, 2006, 11:47am UTC](https://www.ruby-forum.com/t/matrix-modifying-and-coercion-question/67997/1 "2006-09-08T11:47:48Z")

</div>

Hi,  
I was wondering why Ruby Matrix class doesn’t creates objects which are  
editable?

That is,  
m = Matrix[[2,3,4]]  
m[0,0]= 5  
NoMethodError: undefined method `[]=’ for Matrix[[2, 3, 4]]:Matrix  
from (irb):11  
gives error,

as a workaround I have seen people/myself opened the matrix class and  
defined the  
‘[]=’ method such that, ‘@rows’ variable is edited.

```
def []=(i,j,val)
  @rows[i][j] = val
end

```

Is it safe to do this?  
Ditto for the Vector class? why are objects of these classes not  
editable?

Second, if I want to change the class of an object? Is it possible to do  
so?  
Say, I want an object of Matrix class to automatically changed to  
DMatrix,  
which is a derived class, then how do I do so?

Thanks for your help.

## Regards,

Surendra S.  
[http://ssinghi.kreeti.com](http://ssinghi.kreeti.com), [http://www.kreeti.com](http://www.kreeti.com)  
Read my blog at: [http://cuttingtheredtape.blogspot.com/](http://cuttingtheredtape.blogspot.com/)  
,----  
| Great wits are sure to madness near allied,  
| And thin partitions do their bounds divide.  
|  
| (John Dryden, Absalom and Achitophel, 1681)  
`----

---

<div class="post-metadata">

**Author:** ![surendra\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@surendra\_s](https://www.ruby-forum.com/u/surendra_s)\
**Post date:** [September 10, 2006, 5:12pm UTC](https://www.ruby-forum.com/t/matrix-modifying-and-coercion-question/67997/2 "2006-09-10T17:12:54Z")

</div>

Hi,  
I know it is bad posting on one’s own email, but I didn’t get any  
answer. I  
feel these are reasonable questions to ask. Please help.

Regards,

Surendra S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> Second, if I want to change the class of an object? Is it possible to do so?  
> Say, I want an object of Matrix class to automatically changed to DMatrix,  
> which is a derived class, then how do I do so?
> 
> Thanks for your help.

–  
Surendra S.  
[http://ssinghi.kreeti.com](http://ssinghi.kreeti.com), [http://www.kreeti.com](http://www.kreeti.com)  
Read my blog at: [http://cuttingtheredtape.blogspot.com/](http://cuttingtheredtape.blogspot.com/)  
,----  
| “War is Peace! Freedom is Slavery! Ignorance is Strength!”  
| – Orwell, 1984, 1948  
`----
