Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class String
- #
- # String#word_slice
- #
- # As original String::slice but tries to cut whole words instead of characters.
- # How many extra extra chars that should be allowed is specified by the second argument.
- #
- # "A long article that needs a nice cut".slice(1..10) # => "A long arti"
- #
- # "A long article that needs a nice cut".word_slice(1..10) # => "A long article"
- #
- def word_slice(range, max_extra_chars = 20)
- offset = range.max + 1
- string = self.slice(range)
- if self[offset,max_extra_chars] =~ /(^[a-zA-Z0-9]+)/
- string << $1
- end
- string
- end
- end
Add Comment
Please, Sign In to add comment