Tbpgr Blog

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

Ruby | String | center

概要

String#center(width, padding = ' ') -> String

詳細

長さ width の文字列に self を中央寄せした文字列を返す。

サンプルコード
require 'tbpgr_utils'

bulk_puts_eval binding, <<-EOS
'hoge'.center(8)
'hoge'.center(8, '!')
'hoge'.center(8, '+-')
'hoge'.center(4, ':')
EOS

__END__
下記はTbpgrUtils gemの機能
bulk_puts_eval

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

出力

'hoge'.center(8)       # => "  hoge  "
'hoge'.center(8, '!')  # => "!!hoge!!"
'hoge'.center(8, '+-') # => "+-hoge+-"
'hoge'.center(4, ':')  # => "hoge"