Sha256: fccc793275e558302fc035ff5ccb393b2cf2251344856a0d32ef353b04a44ffc

Contents?: true

Size: 1.4 KB

Versions: 23

Compression:

Stored size: 1.4 KB

Contents

module Comparable
  def self.normalize(what)
    return what if Integer === what

    return  1 if what > 0
    return -1 if what < 0
    return  0
  end

  def ==(other)
    return true if equal?(other)

    %x{
      if (self["$<=>"] == Opal.Kernel["$<=>"]) {
        return false;
      }

      // check for infinite recursion
      if (self.$$comparable) {
        delete self.$$comparable;
        return false;
      }
    }

    return false unless cmp = (self <=> other)

    return `#{Comparable.normalize(cmp)} == 0`
  rescue StandardError
    false
  end

  def >(other)
    unless cmp = (self <=> other)
      raise ArgumentError, "comparison of #{self.class} with #{other.class} failed"
    end

    `#{Comparable.normalize(cmp)} > 0`
  end

  def >=(other)
    unless cmp = (self <=> other)
      raise ArgumentError, "comparison of #{self.class} with #{other.class} failed"
    end

    `#{Comparable.normalize(cmp)} >= 0`
  end

  def <(other)
    unless cmp = (self <=> other)
      raise ArgumentError, "comparison of #{self.class} with #{other.class} failed"
    end

    `#{Comparable.normalize(cmp)} < 0`
  end

  def <=(other)
    unless cmp = (self <=> other)
      raise ArgumentError, "comparison of #{self.class} with #{other.class} failed"
    end

    `#{Comparable.normalize(cmp)} <= 0`
  end

  def between?(min, max)
    return false if self < min
    return false if self > max
    return true
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
opal-0.10.6 opal/corelib/comparable.rb
opal-0.10.6.beta opal/corelib/comparable.rb
opal-0.10.5 opal/corelib/comparable.rb
opal-0.10.4 opal/corelib/comparable.rb
opal-0.11.0.rc1 opal/corelib/comparable.rb
opal-0.10.3 opal/corelib/comparable.rb
opal-0.10.2 opal/corelib/comparable.rb
opal-0.10.1 opal/corelib/comparable.rb
opal-0.10.0 opal/corelib/comparable.rb
opal-0.10.0.rc2 opal/corelib/comparable.rb
opal-0.9.4 opal/corelib/comparable.rb
opal-0.9.3 opal/corelib/comparable.rb
opal-0.10.0.rc1 opal/corelib/comparable.rb
opal-0.10.0.beta5 opal/corelib/comparable.rb
opal-0.10.0.beta4 opal/corelib/comparable.rb
opal-0.10.0.beta3 opal/corelib/comparable.rb
opal-0.10.0.beta2 opal/corelib/comparable.rb
opal-0.10.0.beta1 opal/corelib/comparable.rb
opal-0.9.2 opal/corelib/comparable.rb
opal-0.9.0 opal/corelib/comparable.rb