Skip to content

Commit 315e895

Browse files
committed
revises the documentation of String#truncate and the truncate helper
1 parent 5a0d73f commit 315e895

File tree

2 files changed

+18
-22
lines changed

2 files changed

+18
-22
lines changed

actionpack/lib/action_view/helpers/text_helper.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,25 @@ def safe_concat(string)
4343
# ==== Examples
4444
#
4545
# truncate("Once upon a time in a world far far away")
46-
# # => Once upon a time in a worl...
46+
# # => "Once upon a time in a world..."
4747
#
48-
# truncate("Once upon a time in a world far far away", :separator => ' ')
49-
# # => Once upon a time in a world...
48+
# truncate("Once upon a time in a world far far away", :length => 17)
49+
# # => "Once upon a ti..."
5050
#
51-
# truncate("Once upon a time in a world far far away", :length => 14)
52-
# # => Once upon a...
51+
# truncate("Once upon a time in a world far far away", :lenght => 17, :separator => ' ')
52+
# # => "Once upon a..."
5353
#
54-
# truncate("And they found that many people were sleeping better.", :omission => "... (continued)", :length => 25)
55-
# # => And they f... (continued)
54+
# truncate("And they found that many people were sleeping better.", :length => 25, :omission => '... (continued)')
55+
# # => "And they f... (continued)"
5656
#
5757
# You can still use <tt>truncate</tt> with the old API that accepts the
5858
# +length+ as its optional second and the +ellipsis+ as its
5959
# optional third parameter:
6060
# truncate("Once upon a time in a world far far away", 14)
61-
# # => Once upon a...
61+
# # => "Once upon a..."
6262
#
6363
# truncate("And they found that many people were sleeping better.", 25, "... (continued)")
64-
# # => And they f... (continued)
64+
# # => "And they f... (continued)"
6565
def truncate(text, *args)
6666
options = args.extract_options!
6767
unless args.empty?

activesupport/lib/active_support/core_ext/string/filters.rb

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,21 @@ def squish!
2020
self
2121
end
2222

23-
# Truncates a given +text+ after a given <tt>length</tt> if +text+ is longer than <tt>length</tt>.
24-
# The last characters will be replaced with the <tt>:omission</tt> (defaults to "...")
25-
# for a total length not exceeding <tt>:length</tt>.
23+
# Truncates a given +text+ after a given <tt>length</tt> if +text+ is longer than <tt>length</tt>:
2624
#
27-
# Pass a <tt>:separator</tt> to truncate +text+ at a natural break.
25+
# "Once upon a time in a world far far away".truncate(27)
26+
# # => "Once upon a time in a wo..."
2827
#
29-
# ==== Examples
28+
# The last characters will be replaced with the <tt>:omission</tt> string (defaults to "...")
29+
# for a total length not exceeding <tt>:length</tt>:
3030
#
31-
# "Once upon a time in a world far far away".truncate(30)
32-
# # => Once upon a time in a worl...
31+
# "Once upon a time in a world far far away".truncate(27, :separator => ' ')
32+
# # => "Once upon a time in a..."
3333
#
34-
# "Once upon a time in a world far far away".truncate(30, :separator => ' ')
35-
# # => Once upon a time in a world...
36-
#
37-
# "Once upon a time in a world far far away".truncate(14)
38-
# # => Once upon a...
34+
# Pass a <tt>:separator</tt> to truncate +text+ at a natural break:
3935
#
4036
# "And they found that many people were sleeping better.".truncate(25, :omission => "... (continued)")
41-
# # => And they f... (continued)
37+
# # => "And they f... (continued)"
4238
def truncate(length, options = {})
4339
text = self.dup
4440
options[:omission] ||= "..."

0 commit comments

Comments
 (0)