全半角混在揃え(バイト指定)※center追加 #{ruby1.9}

メール投稿テスト(続き)

String#rjustb, String#ljustb に続き、String#centerb の実装です。

2回の投稿にわけた理由は特になしw
そもそもメール投稿テスト用ネタなので。

class String
  def centerb(width, padding = ' ')
    center(width-(bytesize-size), padding)
  end
end

# 比較出力

def disp(strs, title="")
  puts (" %s "%title).center(40, '*') if title
  puts
  strs.each{|str| yield str}
  puts
end

disp(%w{1234}, "【 centerとの動作比較 】") do |str|
  p "|center |" << str.center(10)
  p "|centerb|" << str.centerb(10)
end

disp(%w{1234 1234 1234 1234 1234}, "【 centerbの動作 】") do |str|
  p str.centerb(10)
end

それにしても、String#center, String#rjust, String#ljust の動作は
仕方ないんですかね?