Sha256: b903bc874f944f824bb06a615a65faf6d9964d8d3e4af36e5b3f93c4b417e45c

Contents?: true

Size: 834 Bytes

Versions: 26

Compression:

Stored size: 834 Bytes

Contents

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
  #
  def cmp(other)
    return -1 if length < other.length
    return 1 if length > other.length
    self <=> other  # alphabetic compare
  end

end


#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#
=begin test

  require 'test/unit'

  class TCString < Test::Unit::TestCase

    def test_cmp
      assert_equal( 0, "abc".cmp("abc") )
      assert_equal( -1, "abc".cmp("abcd") )
      assert_equal( 1, "abcd".cmp("abc") )
      assert_equal( -1, "abc".cmp("bcd") )
      assert_equal( 1, "bcd".cmp("abc") )
    end

  end

=end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
facets-0.9.0 lib/nano/string/cmp.rb
facets-1.0.0 lib/facet/string/cmp.rb
facets-1.0.3 packages/core/lib/facet/string/cmp.rb
facets-1.2.1 lib/facets/core/string/cmp.rb
facets-1.1.0 lib/facet/string/cmp.rb
facets-1.3.0 lib/facets/core/string/cmp.rb
facets-1.2.0 lib/facets/core/string/cmp.rb
facets-1.3.1 lib/facets/core/string/cmp.rb
facets-1.3.3 lib/facets/core/string/cmp.rb
facets-1.3.2 lib/facets/core/string/cmp.rb
facets-1.4.1 lib/facets/core/string/cmp.rb
facets-1.4.0 lib/facets/core/string/cmp.rb
facets-1.4.2 lib/facets/core/string/cmp.rb
facets-1.4.3 lib/facets/core/string/cmp.rb
facets-1.4.4 lib/facets/core/string/cmp.rb
facets-1.4.5 lib/facets/core/string/cmp.rb
facets-1.7.38 lib/facets/core/string/cmp.rb
facets-1.7.30 lib/facets/core/string/cmp.rb
facets-1.7.0 lib/facets/core/string/cmp.rb
facets-1.7.46 lib/facets/core/string/cmp.rb