Module: StringMagic::Formatting::Truncation
- Included in:
- StringMagic
- Defined in:
- lib/string_magic/formatting/truncation.rb
Instance Method Summary collapse
- #truncate_sentences(text, count, options = {}) ⇒ Object
- #truncate_words(text, count, options = {}) ⇒ Object
Instance Method Details
#truncate_sentences(text, count, options = {}) ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/string_magic/formatting/truncation.rb', line 14 def truncate_sentences(text, count, = {}) return text if text.nil? || count.nil? || count < 1 suffix = [:suffix] || "..." sentences = text.split(/(?<=[.!?])\s+/) return text if sentences.length <= count sentences[0...count].join(" ") + suffix end |
#truncate_words(text, count, options = {}) ⇒ Object
4 5 6 7 8 9 10 11 12 |
# File 'lib/string_magic/formatting/truncation.rb', line 4 def truncate_words(text, count, = {}) return text if text.nil? || count.nil? || count < 1 suffix = [:suffix] || "..." words = text.split(/\s+/) return text if words.length <= count words[0...count].join(" ") + suffix end |