Guest User

Untitled

a guest
Jan 23rd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.51 KB | None | 0 0
  1. # drop this in a ruby file in my_rails_app/config/initializers
  2. # restart your rails and app you're good to go!
  3.  
  4. class String
  5. # remove middle from strings exceeding max length.
  6. def ellipsize(options={})
  7. max = options[:max] || 40
  8. delimiter = options[:delimiter] || "..."
  9. return self if self.size <= max
  10. remainder = max - delimiter.size
  11. offset = remainder / 2
  12. (self[0,offset + (remainder.odd? ? 1 : 0)].to_s + delimiter + self[-offset,offset].to_s)[0,max].to_s
  13. end unless defined? ellipsize
  14. end
Add Comment
Please, Sign In to add comment