method

truncate_words

Importance_2
Ruby on Rails latest stable (v7.1.3.2) - 0 notes - Class: String
truncate_words(words_count, options = {}) public

Truncates a given text after a given number of words (words_count):

'Once upon a time in a world far far away'.truncate_words(4)
# => "Once upon a time..."

Pass a string or regexp :separator to specify a different separator of words:

'Once<br>upon<br>a<br>time<br>in<br>a<br>world'.truncate_words(5, separator: '<br>')
# => "Once<br>upon<br>a<br>time<br>in..."

The last characters will be replaced with the :omission string (defaults to “…”):

'And they found that many people were sleeping better.'.truncate_words(5, omission: '... (continued)')
# => "And they found that many... (continued)"
Show source
Register or log in to add new notes.