Sha256: d81cf8b55961dc3c6f2ac2ebc881d159cbb1b6affb7e5219840943c48c50d787

Contents?: true

Size: 750 Bytes

Versions: 10

Compression:

Stored size: 750 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

  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 10 versions & 1 rubygems

Version Path
facets-2.8.4 lib/core/facets/comparable/cmp.rb
facets-2.8.3 lib/core/facets/comparable/cmp.rb
facets-2.8.2 lib/core/facets/comparable/cmp.rb
facets-2.8.1 lib/core/facets/comparable/cmp.rb
facets-2.8.0 lib/core/facets/comparable/cmp.rb
facets-2.7.0 lib/core/facets/comparable/cmp.rb
facets-2.6.0 lib/core/facets/comparable/cmp.rb
facets-2.5.0 lib/core/facets/comparable/cmp.rb
facets-2.5.1 lib/core/facets/comparable/cmp.rb
facets-2.5.2 lib/core/facets/comparable/cmp.rb