Tbpgr Blog

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

Ruby | String | *

概要

String#* times -> String

詳細

文字列の内容を times 回だけ繰り返した新しい文字列を返却。

サンプルコード
require 'tbpgr_utils'

bf =->(x) { '>%s.' % (?+ * x.ord) }

bulk_puts_eval binding, <<-EOS
'hoge'*0
'hoge'*2
?+*3
'+' '-'*3
'+%s-' % '1' * 3
'hoge'.chars.map { |e|bf[e] }
EOS

__END__
下記はTbpgrUtils gemの機能
bulk_puts_eval

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

出力

'hoge'*0                      # => ""
'hoge'*2                      # => "hogehoge"
?+*3                          # => "+++"
'+' '-'*3                     # => "+-+-+-"
'+%s-' % '1' * 3              # => "+1-+1-+1-"
'hoge'.chars.map { |e|bf[e] } # => [">++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.", ">+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.", ">+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.", ">+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++."]