small rtruncate refactoring

This commit is contained in:
Alexander Machehin 2013-06-05 15:26:58 +06:00
parent 8ebf36dcb8
commit 0fafc00b82
1 changed files with 3 additions and 3 deletions

View File

@ -24,13 +24,13 @@ class String
# same as reverse.truncate.reverse
def rtruncate(length, options = {})
text = self.dup
chars = self.dup.mb_chars
return self if chars.length <= length
options[:omission] ||= "..."
options[:separator] ||= '/'
chars = text.mb_chars
start = chars.length - length + options[:omission].mb_chars.length
stop = options[:separator] ? (chars.index(options[:separator].mb_chars, start) || start) : start
(chars.length > length ? "#{options[:omission]}#{chars[stop..-1]}" : text).to_s
"#{options[:omission]}#{chars[stop..-1]}".to_s
end
end