Assignment operator on Vector

H

Hans Sjunnesson

I'm fairly new to Ruby and I've just come across something I find odd.
I can't seem to assign a value to an element of a Vector.

The following snippet:

require 'matrix'
v = Vector[10, 20, 30]
v[0] = 30

Will give a NoMethodError exception like so:
NoMethodError: undefined method `[]=' for Vector[10, 20, 30]:Vector
from (irb):3

So, is this intentional design? Is the idea to use Arrays when you want
to modify initial elements?
 
R

Robert Klemme

Hans said:
I'm fairly new to Ruby and I've just come across something I find odd.
I can't seem to assign a value to an element of a Vector.

The following snippet:

require 'matrix'
v = Vector[10, 20, 30]
v[0] = 30

Will give a NoMethodError exception like so:
NoMethodError: undefined method `[]=' for Vector[10, 20, 30]:Vector
from (irb):3

So, is this intentional design?

I guess Vector is meant as an immutable class. If you check the interface
doc at http://www.ruby-doc.org/stdlib/libdoc/matrix/rdoc/index.html it
states that Vector represents a mathematical vector - and there are no
modifying methods. But you can do math with them:
Vector[1,2,3,4] + Vector[0,0,10,0]
=> Vector[1, 2, 13, 4]

This is a new object.
Is the idea to use Arrays when you
want to modify initial elements?

If you don't want to do math then sticking with Array is probably the best
solution. Btw, what do you mean by "initial elements"?

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top