Tbpgr Blog

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

ActiveSupport | String#pluralize

概要

String#pluralize

詳細

String#pluralize について

String#pluralize

文字列の複数形を返却する。
plural は「複数」の意。

引数に数値を与えた場合、1の場合は単数形。それ以外は複数形を返却します。
何かの結果サイズによって文言を変更したい時などに利用することを想定しているようです。

サンプル

# encoding: utf-8
require 'active_support/core_ext/string/inflections'
require 'tbpgr_utils'

bulk_puts_eval binding, <<-EOS
'head'.pluralize
'head'.pluralize 0
'head'.pluralize 1
'head'.pluralize 2
'body'.pluralize
'people'.pluralize
EOS

__END__
・下記はTbpgrUtils gemの機能
bulk_puts_eval

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

出力

'head'.pluralize # => "heads"
'head'.pluralize 0 # => "heads"
'head'.pluralize 1 # => "head"
'head'.pluralize 2 # => "heads"
'head'.pluralize 3 # => "heads"
'body'.pluralize # => "bodies"
'people'.pluralize # => "people"