Sha256: 9af9105e4c0d37293e63deb12df085d7d3c8e741cc4467668eadbfbc90c70232

Contents?: true

Size: 1.18 KB

Versions: 14

Compression:

Stored size: 1.18 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)

    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

14 entries across 14 versions & 2 rubygems

Version Path
opal-0.8.1 opal/corelib/comparable.rb
opal-0.8.1.rc1 opal/corelib/comparable.rb
opal-wedge-0.9.0.dev opal/corelib/comparable.rb
opal-0.8.0 opal/corelib/comparable.rb
opal-0.8.0.rc3 opal/corelib/comparable.rb
opal-0.8.0.rc2 opal/corelib/comparable.rb
opal-0.8.0.rc1 opal/corelib/comparable.rb
opal-0.8.0.beta1 opal/corelib/comparable.rb
opal-0.7.2 opal/corelib/comparable.rb
opal-0.7.1 opal/corelib/comparable.rb
opal-0.7.0 opal/corelib/comparable.rb
opal-0.7.0.rc1 opal/corelib/comparable.rb
opal-0.7.0.beta3 opal/corelib/comparable.rb
opal-0.7.0.beta2 opal/corelib/comparable.rb