Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Ruby | String | casecmp

概要

String#casecmp(other) -> Integer | nil

詳細

String#<=> と同様の比較をするが, 大文字小文字を無視する。

サンプルコード
require 'tbpgr_utils'

bulk_puts_eval binding, <<-EOS
'b'<=>'A'
'b'<=>'a'
'b'<=>'B'
'b'<=>'b'
'b'<=>'C'
'b'<=>'c'
'b'.casecmp('A')
'b'.casecmp('a')
'b'.casecmp('B')
'b'.casecmp('b')
'b'.casecmp('C')
'b'.casecmp('c')
EOS

__END__
下記はTbpgrUtils gemの機能
bulk_puts_eval

https://rubygems.org/gems/tbpgr_utils
https://github.com/tbpgr/tbpgr_utils

出力

'b'<=>'A'        # => 1
'b'<=>'a'        # => 1
'b'<=>'B'        # => 1
'b'<=>'b'        # => 0
'b'<=>'C'        # => 1
'b'<=>'c'        # => -1
'b'.casecmp('A') # => 1
'b'.casecmp('a') # => 1
'b'.casecmp('B') # => 0
'b'.casecmp('b') # => 0
'b'.casecmp('C') # => -1
'b'.casecmp('c') # => -1