Tbpgr Blog

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

Ruby | String | %

概要

String#%

詳細

printf と同じ規則に従って args をフォーマットする。

サンプルコード
require 'tbpgr_utils'

bulk_puts_eval binding, <<-EOS
'hello %s world' % 'Ruby'
'hello %s world' % 'Java'
'%-10s' % 'AWK'
'%-10s' % 'C'
'%#10s' % 'Perl'
'%#10s' % 'Python'
'%s,%s,%s' % ['C', 'C++', 'C#']
EOS

__END__
下記はTbpgrUtils gemの機能
bulk_puts_eval

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

出力

'hello %s world' % 'Ruby'        # => "hello Ruby world"
'hello %s world' % 'Java'        # => "hello Java world"
'%-10s' % 'AWK'                  # => "AWK       "
'%-10s' % 'C'                    # => "C         "
'%#10s' % 'Perl'                 # => "      Perl"
'%#10s' % 'Python'               # => "    Python"
'%s,%s,%s' % ['C', 'C++', 'C#']  # => "C,C++,C#"