Tbpgr Blog

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

ActiveSupport | String#demodulize

概要

String#demodulize

詳細

String#demodulize について

String#demodulize

モジュール::モジュール::任意の名称,などの文字列から右端の任意の名称を取得する。

サンプル

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

bulk_puts_eval binding, <<-EOS
'Module::Class'.demodulize
'Module::Module::Class'.demodulize
'Class'.demodulize
EOS

__END__
・下記はTbpgrUtils gemの機能
bulk_puts_eval

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

出力

'Module::Class'.demodulize # => "Class"
'Module::Module::Class'.demodulize # => "Class"
'Class'.demodulize # => "Class"