kiyo_hikoのブログ

メモ+日記?

Rubyでゼロ埋め文字列 (printf %0s的な何か)

なんとなく必要になって書きました。

def printf_0n(n, str)
  n <= str.length and return str
  "#{1.upto(n - str.length).map{0}.join}#{s}"
end