Sha256: 4ae9eab6240afdd48809ac311203839f51e203613f79eb24058cf35c64289012

Contents?: true

Size: 1.79 KB

Versions: 13

Compression:

Stored size: 1.79 KB

Contents

module Kl
  module Primitives
    module Arithmetic
      def +(a, b)
        raise ::Kl::Error, "#{a} is not a number" unless a.kind_of? Numeric
        raise ::Kl::Error, "#{b} is not a number" unless b.kind_of? Numeric
        a + b
      end
      
      def -(a, b)
        raise ::Kl::Error, "#{a} is not a number" unless a.kind_of? Numeric
        raise ::Kl::Error, "#{b} is not a number" unless b.kind_of? Numeric
        a - b
      end
      
      def *(a, b)
        raise ::Kl::Error, "#{a} is not a number" unless a.kind_of? Numeric
        raise ::Kl::Error, "#{b} is not a number" unless b.kind_of? Numeric
        a * b
      end
      
      def /(a, b)
        raise ::Kl::Error, "#{a} is not a number" unless a.kind_of? Numeric
        raise ::Kl::Error, "#{b} is not a number" unless b.kind_of? Numeric
        if a.kind_of?(Fixnum) && b.kind_of?(Fixnum) && a % b != 0
          a = a.to_f
        end
        a / b
      end
      
      def >(a, b)
        raise ::Kl::Error, "#{a} is not a number" unless a.kind_of? Numeric
        raise ::Kl::Error, "#{b} is not a number" unless b.kind_of? Numeric
        a > b
      end

      def <(a, b)
        raise ::Kl::Error, "#{a} is not a number" unless a.kind_of? Numeric
        raise ::Kl::Error, "#{b} is not a number" unless b.kind_of? Numeric
        a < b
      end
      
      def >=(a, b)
        raise ::Kl::Error, "#{a} is not a number" unless a.kind_of? Numeric
        raise ::Kl::Error, "#{b} is not a number" unless b.kind_of? Numeric
        a >= b
      end
      
      def <=(a, b)
        raise ::Kl::Error, "#{a} is not a number" unless a.kind_of? Numeric
        raise ::Kl::Error, "#{b} is not a number" unless b.kind_of? Numeric
        a <= b
      end
      
      def number?(a)
        a.kind_of?(Numeric)
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
shen-ruby-0.10.0 lib/kl/primitives/arithmetic.rb
shen-ruby-0.9.0 lib/kl/primitives/arithmetic.rb
shen-ruby-0.8.1 lib/kl/primitives/arithmetic.rb
shen-ruby-0.8.0 lib/kl/primitives/arithmetic.rb
shen-ruby-0.7.0 lib/kl/primitives/arithmetic.rb
shen-ruby-0.6.0 lib/kl/primitives/arithmetic.rb
shen-ruby-0.5.0 lib/kl/primitives/arithmetic.rb
shen-ruby-0.4.1 lib/kl/primitives/arithmetic.rb
shen-ruby-0.4.0 lib/kl/primitives/arithmetic.rb
shen-ruby-0.3.1 lib/kl/primitives/arithmetic.rb
shen-ruby-0.3.0 lib/kl/primitives/arithmetic.rb
shen-ruby-0.2.0 lib/kl/primitives/arithmetic.rb
shen-ruby-0.1.0 lib/kl/primitives/arithmetic.rb