1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

revises the documentation of String#truncate and the truncate helper

This commit is contained in:
Xavier Noria 2010-06-02 01:34:39 +02:00
parent 5a0d73f17c
commit 315e8952df
2 changed files with 18 additions and 22 deletions

View file

@ -43,25 +43,25 @@ module ActionView
# ==== Examples # ==== Examples
# #
# truncate("Once upon a time in a world far far away") # truncate("Once upon a time in a world far far away")
# # => Once upon a time in a worl... # # => "Once upon a time in a world..."
# #
# truncate("Once upon a time in a world far far away", :separator => ' ') # truncate("Once upon a time in a world far far away", :length => 17)
# # => Once upon a time in a world... # # => "Once upon a ti..."
# #
# truncate("Once upon a time in a world far far away", :length => 14) # truncate("Once upon a time in a world far far away", :lenght => 17, :separator => ' ')
# # => Once upon a... # # => "Once upon a..."
# #
# truncate("And they found that many people were sleeping better.", :omission => "... (continued)", :length => 25) # truncate("And they found that many people were sleeping better.", :length => 25, :omission => '... (continued)')
# # => And they f... (continued) # # => "And they f... (continued)"
# #
# You can still use <tt>truncate</tt> with the old API that accepts the # You can still use <tt>truncate</tt> with the old API that accepts the
# +length+ as its optional second and the +ellipsis+ as its # +length+ as its optional second and the +ellipsis+ as its
# optional third parameter: # optional third parameter:
# truncate("Once upon a time in a world far far away", 14) # truncate("Once upon a time in a world far far away", 14)
# # => Once upon a... # # => "Once upon a..."
# #
# truncate("And they found that many people were sleeping better.", 25, "... (continued)") # truncate("And they found that many people were sleeping better.", 25, "... (continued)")
# # => And they f... (continued) # # => "And they f... (continued)"
def truncate(text, *args) def truncate(text, *args)
options = args.extract_options! options = args.extract_options!
unless args.empty? unless args.empty?

View file

@ -20,25 +20,21 @@ class String
self self
end end
# Truncates a given +text+ after a given <tt>length</tt> if +text+ is longer than <tt>length</tt>. # Truncates a given +text+ after a given <tt>length</tt> if +text+ is longer than <tt>length</tt>:
# The last characters will be replaced with the <tt>:omission</tt> (defaults to "...")
# for a total length not exceeding <tt>:length</tt>.
# #
# Pass a <tt>:separator</tt> to truncate +text+ at a natural break. # "Once upon a time in a world far far away".truncate(27)
# # => "Once upon a time in a wo..."
# #
# ==== Examples # The last characters will be replaced with the <tt>:omission</tt> string (defaults to "...")
# for a total length not exceeding <tt>:length</tt>:
# #
# "Once upon a time in a world far far away".truncate(30) # "Once upon a time in a world far far away".truncate(27, :separator => ' ')
# # => Once upon a time in a worl... # # => "Once upon a time in a..."
# #
# "Once upon a time in a world far far away".truncate(30, :separator => ' ') # Pass a <tt>:separator</tt> to truncate +text+ at a natural break:
# # => Once upon a time in a world...
#
# "Once upon a time in a world far far away".truncate(14)
# # => Once upon a...
# #
# "And they found that many people were sleeping better.".truncate(25, :omission => "... (continued)") # "And they found that many people were sleeping better.".truncate(25, :omission => "... (continued)")
# # => And they f... (continued) # # => "And they f... (continued)"
def truncate(length, options = {}) def truncate(length, options = {})
text = self.dup text = self.dup
options[:omission] ||= "..." options[:omission] ||= "..."