From 4122c320bb8a81ff66eae72e25eda8af6c768683 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Tue, 17 Oct 2017 13:26:37 +0900 Subject: Add `{String,Symbol}#casecmp?`; CRuby2.4 --- mrbgems/mruby-string-ext/mrblib/string.rb | 15 ++++++++++++++- mrbgems/mruby-symbol-ext/mrblib/symbol.rb | 15 ++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/mrbgems/mruby-string-ext/mrblib/string.rb b/mrbgems/mruby-string-ext/mrblib/string.rb index c3a7eb380..cb36d9a48 100644 --- a/mrbgems/mruby-string-ext/mrblib/string.rb +++ b/mrbgems/mruby-string-ext/mrblib/string.rb @@ -144,7 +144,20 @@ class String def casecmp(str) self.downcase <=> str.to_str.downcase 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 def partition(sep) diff --git a/mrbgems/mruby-symbol-ext/mrblib/symbol.rb b/mrbgems/mruby-symbol-ext/mrblib/symbol.rb index 1e3d24b80..28cce3156 100644 --- a/mrbgems/mruby-symbol-ext/mrblib/symbol.rb +++ b/mrbgems/mruby-symbol-ext/mrblib/symbol.rb @@ -48,10 +48,23 @@ class Symbol def casecmp(other) return nil unless other.kind_of?(Symbol) lhs = self.to_s; lhs.upcase! - rhs = other.to_s; rhs.upcase! + rhs = other.to_s.upcase lhs <=> rhs 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: # sym.empty? -> true or false -- cgit v1.2.3