Commit a6d8df67 authored by Yukihiro "Matz" Matsumoto's avatar Yukihiro "Matz" Matsumoto

Merge pull request #2088 from ksss/string-casecmp

String#casecmp should be call `to_str`
parents bd75406a 617fa702
...@@ -47,7 +47,9 @@ class String ...@@ -47,7 +47,9 @@ class String
# "abcdef".casecmp("ABCDEF") #=> 0 # "abcdef".casecmp("ABCDEF") #=> 0
# #
def casecmp(str) 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 end
def partition(sep) def partition(sep)
......
...@@ -98,6 +98,11 @@ assert('String#casecmp') do ...@@ -98,6 +98,11 @@ assert('String#casecmp') do
assert_equal 0, "aBcDeF".casecmp("abcdef") assert_equal 0, "aBcDeF".casecmp("abcdef")
assert_equal(-1, "abcdef".casecmp("abcdefg")) assert_equal(-1, "abcdef".casecmp("abcdefg"))
assert_equal 0, "abcdef".casecmp("ABCDEF") assert_equal 0, "abcdef".casecmp("ABCDEF")
o = Object.new
def o.to_str
"ABCDEF"
end
assert_equal 0, "abcdef".casecmp(o)
end end
assert('String#start_with?') do assert('String#start_with?') do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment