Tbpgr Blog

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

ActiveSupport | String#camelize

概要

String#camelize

詳細

String#camelize について

String#camelize

文字列をキャメルケースにした結果を受け取る
引数に「:lower」を与えると、最初の文字が小文字になる

サンプル

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

bulk_puts_eval binding, <<-EOS
'snake_case'.camelize
'snake_case'.camelize(:lower)
'lower'.camelize
'UPPER'.camelize
'some_module/some_class'.camelize
EOS

__END__
・下記はTbpgrUtils gemの機能
bulk_puts_eval

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

出力

'snake_case'.camelize # => "SnakeCase"
'snake_case'.camelize(:lower) # => "snakeCase"
'lower'.camelize # => "Lower"
'UPPER'.camelize # => "UPPER"
'some_module/some_class'.camelize # => "SomeModule::SomeClass"