Tbpgr Blog

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

ActiveSupport | String#constantize

概要

String#constantize

詳細

String#constantize について

String#constantize

文字列から定数を取得する

サンプル

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

class Hoge
  HIGE = "hige"
end

bulk_puts_eval binding, <<-EOS
'Hoge'.constantize
'Hoge::HIGE'.constantize
EOS

__END__
・下記はTbpgrUtils gemの機能
bulk_puts_eval

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

出力

'Hoge'.constantize # => Hoge
'Hoge::HIGE'.constantize # => "hige"