Skip to content
Permalink
Browse files

Merge pull request #623 from kachick/implement-string-casecmp

Implement String#casecmp
  • Loading branch information
alex committed Apr 19, 2013
2 parents e38ccea + 71942f7 commit 7209ea0cea51cc6e3f16eebf0582f2b1855b995b
Showing with 22 additions and 17 deletions.
  1. +22 −0 lib-topaz/string.rb
  2. +0 −17 spec/tags/core/string/casecmp_tags.txt
@@ -31,6 +31,28 @@ def capitalize
return copy
end

def casecmp(other)
unless other.respond_to?(:to_str)
raise TypeError.new("can't convert #{other.class} into String")
end

other = other.to_str
diff = self.length - other.length
short = diff < 0
long = diff > 0
limit = (short ? self.length : other.length) - 1

0.upto(limit) do |index|
a, b = self[index], other[index]
a.upcase!
b.upcase!
compared = a <=> b
return compared unless compared == 0
end

short ? -1 : (long ? 1 : 0)
end

def empty?
self.length == 0
end

This file was deleted.

0 comments on commit 7209ea0

Please sign in to comment.
You can’t perform that action at this time.