Sha256: 75955505d977e7cd16a3020ce9f33b58ce9a0e4bbf94e405bf99cbe7da4545b8

Contents?: true

Size: 753 Bytes

Versions: 7

Compression:

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

7 entries across 7 versions & 2 rubygems

Version Path
facets-2.4.0 lib/facets/comparable/cmp.rb
facets-2.4.1 lib/facets/comparable/cmp.rb
facets-2.4.2 lib/core/facets/comparable/cmp.rb
facets-2.4.3 lib/core/facets/comparable/cmp.rb
facets-2.4.4 lib/core/facets/comparable/cmp.rb
facets-2.4.5 lib/core/facets/comparable/cmp.rb
mack-facets-0.8.2 lib/gems/facets-2.4.5/lib/core/facets/comparable/cmp.rb