Sha256: 32895ab5bd114be31a5a80b0a63b704ac756d91278c95a87ba34ba86e867d90e

Contents?: true

Size: 1018 Bytes

Versions: 6

Compression:

Stored size: 1018 Bytes

Contents

require 'bigdecimal'

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

6 entries across 6 versions & 1 rubygems

Version Path
dentaku-1.2.6 lib/dentaku/binary_operation.rb
dentaku-1.2.5 lib/dentaku/binary_operation.rb
dentaku-1.2.4 lib/dentaku/binary_operation.rb
dentaku-1.2.3 lib/dentaku/binary_operation.rb
dentaku-1.2.2 lib/dentaku/binary_operation.rb
dentaku-1.2.1 lib/dentaku/binary_operation.rb