Chapter I.7
Arguments coercion for “Result” class operators
This chapter concerns the ruby extension only!
One class and some ruby methods are directly defined in ruby language. This part of the
programming concerns the operators in which the second argument is a “Result” object, but the first
argument has another type (Real, Complex, Array...). Then, a ruby type coercion is performed thanks
to the “Result” class “coerce” method. This small part of code allows to perform the operations using
the different “Post.op???” methods.
It seems that the built-in “Array” class defines its own “coerce” method, which conflicts with the
“Result” coerce mechanism. Therefore, a modification of the “Array” class is restored in version 4.4.4
of FeResPost. (See below.)
This part of FeResPost is programmed in the “FeResPost.rb” file. content looks as
follows:
# encoding: utf-8
require "FeResPost.so"
module FeResPost
class ROpResult
@x=nil
def initialize(res)
@x=res
end
def +(other)
return Post.opAdd(other,@x)
end
def -(other)
return Post.opSub(other,@x)
end
def *(other)
return Post.opMul(other,@x)
end
def /(other)
return Post.opDiv(other,@x)
end
def **(other)
return Post.pow(other,@x)
end
end
class Result
def coerce(x)
[ROpResult.new(self),x]
end
end # Result
end # FeResPost
class Array
alias _FeFesPost_old_opAdd +
alias _FeFesPost_old_opSub -
alias _FeFesPost_old_opMul *
def +(second)
if second.class==Result then
return Post.opAdd(self,second)
else
return self._FeFesPost_old_opAdd(second)
end
end
def -(second)
if second.class==Result then
return Post.opSub(self,second)
else
return self._FeFesPost_old_opSub(second)
end
end
def *(second)
if second.class==Result then
return Post.opMul(self,second)
else
return self._FeFesPost_old_opMul(second)
end
end
def /(second)
if second.class==Result then
return Post.opDiv(self,second)
else
raise "Invalid second operand for / operator"
end
end
end # Array
puts "End ' "FeResPost' " module initialization.' n"
puts "' n' n"