Created
September 13, 2010 14:01
-
-
Save netguru/577326 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class String | |
def truncate_full_words limit = 64, completion = "..." | |
self.length > limit ? "#{self[0..(self.rindex(' ', limit) || limit)-1]}#{competion}" : self | |
end | |
def truncate_full_words! limit = 64, completion = "..." | |
self.replace(self.truncate_full_words limit, completion) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>> some_string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum nec enim nibh." | |
=> "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum nec enim nibh." | |
>> some_string.truncate_full_words | |
=> "Lorem ipsum dolor sit amet, consectetur adipiscing elit. ..." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment