small rtruncate refactoring
This commit is contained in:
parent
8ebf36dcb8
commit
0fafc00b82
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue