Tbpgr Blog

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

ActiveSupport | String#humanize

概要

String#humanize

詳細

String#humanize について

String#humanize

属性の名前を人が見やすい形式に変換する。
アンダースコアをスペースに変更し、
_idを削除し、
先頭の単語をCapitalizeします。

サンプル

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

class Hoge
  HIGE = "hige"
end

bulk_puts_eval binding, <<-EOS
'person'.humanize
'PERSON'.humanize
'person_id'.humanize
'person_name'.humanize
EOS

__END__
・下記はTbpgrUtils gemの機能
bulk_puts_eval

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

出力

'person'.humanize # => "Person"
'PERSON'.humanize # => "Person"
'person_id'.humanize # => "Person"
'person_name'.humanize # => "Person name"