diff options
Diffstat (limited to 'activesupport/lib/active_support/core_ext/string/filters.rb')
-rw-r--r-- | activesupport/lib/active_support/core_ext/string/filters.rb | 9 |
1 files changed, 6 insertions, 3 deletions
diff --git a/activesupport/lib/active_support/core_ext/string/filters.rb b/activesupport/lib/active_support/core_ext/string/filters.rb index 2029909486..1dfaf76673 100644 --- a/activesupport/lib/active_support/core_ext/string/filters.rb +++ b/activesupport/lib/active_support/core_ext/string/filters.rb @@ -63,7 +63,7 @@ class String "#{self[0, stop]}#{omission}" end - # Truncates a given +text+ after a given number of words<tt>words_count</tt>: + # Truncates a given +text+ after a given number of words (<tt>words_count</tt>): # # 'Once upon a time in a world far far away'.truncate_words(4) # # => "Once upon a time..." @@ -80,7 +80,10 @@ class String def truncate_words(words_count, options = {}) sep = options[:separator] || /\s+/ sep = Regexp.escape(sep.to_s) unless Regexp === sep - return dup unless self =~ /^((?:.+?#{sep}){#{words_count - 1}}.+?)#{sep}.*/ - $1 + (options[:omission] || '...') + if self =~ /\A((?:.+?#{sep}){#{words_count - 1}}.+?)#{sep}.*/m + $1 + (options[:omission] || '...') + else + dup + end end end |