Guest User

Untitled

a guest
Feb 20th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. class String
  2. #
  3. # String#word_slice
  4. #
  5. # As original String::slice but tries to cut whole words instead of characters.
  6. # How many extra extra chars that should be allowed is specified by the second argument.
  7. #
  8. # "A long article that needs a nice cut".slice(1..10) # => "A long arti"
  9. #
  10. # "A long article that needs a nice cut".word_slice(1..10) # => "A long article"
  11. #
  12. def word_slice(range, max_extra_chars = 20)
  13. offset = range.max + 1
  14. string = self.slice(range)
  15.  
  16. if self[offset,max_extra_chars] =~ /(^[a-zA-Z0-9]+)/
  17. string << $1
  18. end
  19.  
  20. string
  21. end
  22. end
Add Comment
Please, Sign In to add comment