Add `{String,Symbol}#casecmp?`; CRuby2.4

parent db8265f4
...@@ -144,7 +144,20 @@ class String ...@@ -144,7 +144,20 @@ class String
def casecmp(str) def casecmp(str)
self.downcase <=> str.to_str.downcase self.downcase <=> str.to_str.downcase
rescue NoMethodError rescue NoMethodError
raise TypeError, "no implicit conversion of #{str.class} into String" nil
end
##
# call-seq:
# str.casecmp?(other) -> true, false, or nil
#
# Returns true if str and other_str are equal after case folding,
# false if they are not equal, and nil if other_str is not a string.
def casecmp?(str)
c = self.casecmp(str)
return nil if c.nil?
return c == 0
end end
def partition(sep) def partition(sep)
......
...@@ -48,10 +48,23 @@ class Symbol ...@@ -48,10 +48,23 @@ class Symbol
def casecmp(other) def casecmp(other)
return nil unless other.kind_of?(Symbol) return nil unless other.kind_of?(Symbol)
lhs = self.to_s; lhs.upcase! lhs = self.to_s; lhs.upcase!
rhs = other.to_s; rhs.upcase! rhs = other.to_s.upcase
lhs <=> rhs lhs <=> rhs
end end
##
# call-seq:
# sym.casecmp?(other) -> true, false, or nil
#
# Returns true if sym and other_sym are equal after case folding,
# false if they are not equal, and nil if other_sym is not a string.
def casecmp?(sym)
c = self.casecmp(sym)
return nil if c.nil?
return c == 0
end
# #
# call-seq: # call-seq:
# sym.empty? -> true or false # sym.empty? -> true or false
......
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