FeResPost Web Site                     FeResPost Online User Manual

Chapter I.7
Arguments coercion for “Result” class operators

This chapter concerns the ruby extension only!

One class and some ruby methods are defined directly in the ruby language. This part of the code handles the operators in which the second argument is a “Result” object, but the first argument has another type (Real, Complex, Array...). In this case, a ruby type coercion is performed via the “Result” class “coerce” method. This small piece of code allows the operations to be performed 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 was introduced in version 4.4.4 of FeResPost. (See below.)

This part of FeResPost is programmed in the “FeResPost.rb” file. Its content is 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 _FeResPost_old_opAdd +
    alias _FeResPost_old_opSub -
    alias _FeResPost_old_opMul *
    def +(second)
        if second.class==Result then
            return Post.opAdd(self,second)
        else
            return self._FeResPost_old_opAdd(second)
        end
    end
    def -(second)
        if second.class==Result then
            return Post.opSub(self,second)
        else
            return self._FeResPost_old_opSub(second)
        end
    end
    def *(second)
        if second.class==Result then
            return Post.opMul(self,second)
        else
            return self._FeResPost_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"