D
Daniel Vartanov
Hello!
I am trying to replace .+ operator of Numeric class if and only if
argument (summand) is my own class.
To be more specific: I am writing geometry module in Ruby and need to
implement vector-number multiplication so that both:
2 * vector
vector * 2
work well.
Therefore I need to replace .+ method of Numeric without spoiling
arithmetical operations with other classes. It is clear, that in case of
inheritance we can just write:
class MyNumeric < Numeric
def +(object)
super(object) unless object.kind_of?(Vector)
# vector-number multiplication here...
end
end
But, of course, we need a replacement here. How to implement it?
class Numeric
def +(vector)
unless vector.kinf_of?(Vector)
# What to write here??!
end
# vector-number multiplication here...
end
I am trying to replace .+ operator of Numeric class if and only if
argument (summand) is my own class.
To be more specific: I am writing geometry module in Ruby and need to
implement vector-number multiplication so that both:
2 * vector
vector * 2
work well.
Therefore I need to replace .+ method of Numeric without spoiling
arithmetical operations with other classes. It is clear, that in case of
inheritance we can just write:
class MyNumeric < Numeric
def +(object)
super(object) unless object.kind_of?(Vector)
# vector-number multiplication here...
end
end
But, of course, we need a replacement here. How to implement it?
class Numeric
def +(vector)
unless vector.kinf_of?(Vector)
# What to write here??!
end
# vector-number multiplication here...
end