diff options
| author | ksss <[email protected]> | 2014-04-20 13:37:29 +0900 |
|---|---|---|
| committer | ksss <[email protected]> | 2014-04-20 13:43:59 +0900 |
| commit | bb52324bd95628b75925693340577d4390a2c564 (patch) | |
| tree | b503d966f0bb4042a090577db9d5a5648f23178b | |
| parent | 44cc51fef7b68f2b93a5c51d4da9778ca43fa80e (diff) | |
| download | mruby-bb52324bd95628b75925693340577d4390a2c564.tar.gz mruby-bb52324bd95628b75925693340577d4390a2c564.zip | |
Implement Symbol#casecmp
| -rw-r--r-- | mrbgems/mruby-symbol-ext/mrblib/symbol.rb | 11 | ||||
| -rw-r--r-- | mrbgems/mruby-symbol-ext/test/symbol.rb | 7 |
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 78edf3cad..e38ee468d 100644 --- a/mrbgems/mruby-symbol-ext/mrblib/symbol.rb +++ b/mrbgems/mruby-symbol-ext/mrblib/symbol.rb @@ -47,6 +47,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 |
