excerpt

excerpt(text, phrase, options = {})
Instance Public methods

Extracts an excerpt from text that matches the first instance of phrase. The :radius option expands the excerpt on each side of the first occurrence of phrase by the number of characters defined in :radius (which defaults to 100). If the excerpt radius overflows the beginning or end of the text, then the :omission option (which defaults to ââ¦â) will be prepended/appended accordingly. Use the :separator option to choose the delimitation. The resulting string will be stripped in any case. If the phrase isn't found, nil is returned.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
excerpt('This is an example', 'an', radius: 5)
# => ...s is an exam...
 
excerpt('This is an example', 'is', radius: 5)
# => This is a...
 
excerpt('This is an example', 'is')
# => This is an example
 
excerpt('This next thing is an example', 'ex', radius: 2)
# => ...next...
 
excerpt('This is also an example', 'an', radius: 8, omission: '<chop> ')
# => <chop> is also an example
 
excerpt('This is a very beautiful morning', 'very', separator: ' ', radius: 1)
# => ...a very beautiful...
doc_ruby_on_rails
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.