半角全角の文字列を見た目上そろえて、切り詰める。
railsのデフォルトのtruncateだと純粋に文字単位で切り詰めるので(当たり前だけど)、
切り詰めた後の見た目上の文字列の長さがばらばらになる。
見た目上行末がだいたいそろう感じにしたら、一覧がきれいに見えるかなと思ったので、
作ってみた。でも、まだどこにも組み込んでないです。
def display_limit(src='', limit_length=30, replace='...') return "" if src.nil? || src.length == 0 return src if limit_length == 0 temp = src.split(//) char_length = temp.length src_size = src.length count = 0 temp.each_with_index do |chr, idx| count += chr.size > 1 ? 2 : 1 if count >= limit_length if count == src_size return src else return temp[0..idx].join('') + replace end end end return src end