Sha256: 2f041764d77472edcb7d60b7720fb631095a74025cfc7e85af34c5978f5d53a8

Contents?: true

Size: 996 Bytes

Versions: 2

Compression:

Stored size: 996 Bytes

Contents

module Dentaku
  class BinaryOperation
    attr_reader :left, :right

    def initialize(left, right)
      @left = left
      @right = right
    end

    def pow;      [:numeric, left ** right]; end
    def add;      [:numeric, left + right];  end
    def subtract; [:numeric, left - right];  end
    def multiply; [:numeric, left * right];  end

    def divide
      quotient, remainder = left.divmod(right)
      return [:numeric, quotient] if remainder == 0
      [:numeric, BigDecimal.new(left.to_s) / BigDecimal.new(right.to_s)]
    end

    def mod;      [:numeric, left % right]; end

    def le;       [:logical, left <= right]; end
    def ge;       [:logical, left >= right]; end
    def lt;       [:logical, left <  right]; end
    def gt;       [:logical, left >  right]; end
    def ne;       [:logical, left != right]; end
    def eq;       [:logical, left == right]; end

    def and;      [:logical, left && right]; end
    def or;       [:logical, left || right]; end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dentaku-1.2.0 lib/dentaku/binary_operation.rb
dentaku-1.1.0 lib/dentaku/binary_operation.rb