I need some help with my +/- overridden method, it is not affecting the elements of wektor array.
class Wektor
attr_accessor :coords
def initialize(length)
@coords = Array.new(length, 0)
end
def set!(w)
@coords = w.dup
self
end
%i(* /).each do |op|
define_method(op) do |n|
coords.map! { |i| i.send(op, n) }
self
end
end
%i(- +).each do |op|
define_method(op) do |v|
@coords.zip(v).map { |a, b| a.send(op, b) }
self
end
end
def shift_right!
coords.rotate!
coords[0] = 0
self
end
end
So basically if a = Wektor.new(4).set!([1,2,3,4] and b = Wektor.new(4).set!([1,1,1,1] I want a-b to set a = [0,1,2,3] What is wrong with this method? It is not changing anything - a is still set to [1,2,3,4].
I tried debugging with IRB but it doesn't give me any clue on what is wrong.
The code looks good to me, but I'm a beginner when it comes to writing ruby-way code (I'm not the author of this piece of code) and I have trouble spotting the error.
* and / is working OK, the vector is supposed to be multiplied/divided by scalar.