summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2014-04-20 21:30:46 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2014-04-20 21:30:46 +0900
commita6941101933a06e234f57a180d7119eafa9f1904 (patch)
tree192aead21f0cab7a50d0d5c9ef5518ad3278013c
parenta6d8df6761a0c84f0cd275a709050c187f60b35f (diff)
parentbb52324bd95628b75925693340577d4390a2c564 (diff)
downloadmruby-a6941101933a06e234f57a180d7119eafa9f1904.tar.gz
mruby-a6941101933a06e234f57a180d7119eafa9f1904.zip
Merge pull request #2089 from ksss/sym-casecmp
Implement Symbol#casecmp
-rw-r--r--mrbgems/mruby-symbol-ext/mrblib/symbol.rb11
-rw-r--r--mrbgems/mruby-symbol-ext/test/symbol.rb7
2 files changed, 18 insertions, 0 deletions
diff --git a/mrbgems/mruby-symbol-ext/mrblib/symbol.rb b/mrbgems/mruby-symbol-ext/mrblib/symbol.rb
index 51a8ddc84..f4bf23cb1 100644
--- a/mrbgems/mruby-symbol-ext/mrblib/symbol.rb
+++ b/mrbgems/mruby-symbol-ext/mrblib/symbol.rb
@@ -48,6 +48,17 @@ class Symbol
self.to_s.upcase.intern
end
+ ##
+ # call-seq:
+ # sym.casecmp(other) -> -1, 0, +1 or nil
+ #
+ # Case-insensitive version of <code>Symbol#<=></code>.
+
+ def casecmp(other)
+ return nil unless other.kind_of?(Symbol)
+ self.to_s.upcase <=> other.to_s.upcase
+ end
+
#
# call-seq:
# sym.empty? -> true or false
diff --git a/mrbgems/mruby-symbol-ext/test/symbol.rb b/mrbgems/mruby-symbol-ext/test/symbol.rb
index 35bb31aef..e886a58d7 100644
--- a/mrbgems/mruby-symbol-ext/test/symbol.rb
+++ b/mrbgems/mruby-symbol-ext/test/symbol.rb
@@ -29,6 +29,13 @@ assert("Symbol#upcase") do
assert_equal :HELLO, :hEllO.upcase
end
+assert("Symbol#casecmp") do
+ assert_equal 0, :HELLO.casecmp(:hEllO)
+ assert_equal 1, :HELLO.casecmp(:hEllN)
+ assert_equal -1, :HELLO.casecmp(:hEllP)
+ assert_nil :HELLO.casecmp("hEllO")
+end
+
assert("Symbol#empty?") do
assert_true :''.empty?
end