Sha256: 0f8820c89e3cc374c3072512f4244a5390b841b04ac6d9d81bce105c756a6307

Contents?: true

Size: 803 Bytes

Versions: 10

Compression:

Stored size: 803 Bytes

Contents

module Comparable

  # Alternate name for comparison operator #<=>.
  #
  #   3.cmp(1)   #=>  1
  #   3.cmp(3)   #=>  0
  #   3.cmp(10)  #=> -1
  #
  # This fundamental compare method is used to keep
  # comparison compatible with <tt>#succ</tt>.
  #
  # CREDIT: Peter Vanbroekhoven

  def cmp(o)
    self<=>o
  end

end

class String

  # Compare method that takes length into account.
  # Unlike #<=>, this is compatible with #succ.
  #
  #   "abc".cmp("abc")   #=>  0
  #   "abcd".cmp("abc")  #=>  1
  #   "abc".cmp("abcd")  #=> -1
  #   "xyz".cmp("abc")   #=>  1
  #
  # CREDIT: Peter Vanbroekhoven
  #
  # TODO: Move String#cmp to string/ directory.

  def cmp(other)
    return -1 if length < other.length
    return 1 if length > other.length
    self <=> other  # alphabetic compare
  end

end

Version data entries

10 entries across 9 versions & 2 rubygems

Version Path
facets-glimmer-3.2.0 lib/core/facets/comparable/cmp.rb
facets-3.1.0 lib/core/facets/comparable/cmp.rb
facets-3.0.0 lib/core/facets/comparable/cmp.rb
facets-2.9.3 lib/core/facets/comparable/cmp.rb
facets-2.9.2 lib/core/facets/comparable/cmp.rb
facets-2.9.2 src/core/facets/comparable/cmp.rb
facets-2.9.1 lib/core/facets/comparable/cmp.rb
facets-2.9.0 lib/core/facets/comparable/cmp.rb
facets-2.9.0.pre.2 lib/core/facets/comparable/cmp.rb
facets-2.9.0.pre.1 lib/core/facets/comparable/cmp.rb