String#casecmp should be call to_str

when non String object sent
And should be raise TypeError
when can not responsed to `to_str`
This commit is contained in:
ksss
2014-04-20 13:06:25 +09:00
parent 44cc51fef7
commit 617fa70211
2 changed files with 8 additions and 1 deletions
+3 -1
View File
@@ -47,7 +47,9 @@ class String
# "abcdef".casecmp("ABCDEF") #=> 0
#
def casecmp(str)
self.downcase <=> str.downcase
self.downcase <=> str.to_str.downcase
rescue NoMethodError
raise TypeError, "no implicit conversion of #{str.class} into String"
end
def partition(sep)
+5
View File
@@ -98,6 +98,11 @@ assert('String#casecmp') do
assert_equal 0, "aBcDeF".casecmp("abcdef")
assert_equal(-1, "abcdef".casecmp("abcdefg"))
assert_equal 0, "abcdef".casecmp("ABCDEF")
o = Object.new
def o.to_str
"ABCDEF"
end
assert_equal 0, "abcdef".casecmp(o)
end
assert('String#start_with?') do