f l a m e . o r g

organized flames

String#bitruncate

Posted on February 21, 2009

And that’s ‘“bi-truncate” not “bit-uncate”.

What’s it do?

1 "This is a test".bitruncate(:length => 6) ==> "Thi...est"
2 "This is a test".bitruncate(:elength => 6) ==> "...a test"

The default options are { :length => 30 } which will produce 15 characters from the front and 15 from the end, putting … marks in the middle.

For rails, I put this in my lib/core_extensions.rb file.

 1 #
 2 # Copyright (c) 2009 Michael Graff.  All rights reserved.
 3 #
 4 # Redistribution and use in source and binary forms, with or
 5 # without modification, are permitted provided that the following
 6 # conditions are met:
 7 # 1. Redistributions of source code must retain the above copyright
 8 #    notice, this list of conditions and the following disclaimer.
 9 # 2. Redistributions in binary form must reproduce the above
10 #    copyright notice, this list of conditions and the following
11 #    disclaimer in the documentation and/or other materials provided
12 #    with the distribution.
13 # 3. The name of Michael Graff may not be used to endorse or promote
14 #    products derived from this software without specific prior
15 #    written permission.
16 #
17 # THIS SOFTWARE IS PROVIDED BY Michael Graff ``AS IS'' AND ANY
18 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19 # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 # PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL Micahel Graff
21 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
23 # TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
28 # OF SUCH DAMAGE.
29 #
30 
31 class String
32   #
33   # Truncate from both ends of a string.  The :length parameter, which defaults
34   # to 30, will return the first 15 and the last 15 characters from a string
35   # if it is longer than 30 characters.  If it is shorter, the entire string
36   # is returned.
37   #
38   # Another way to specify the front and back portions are with :flength and
39   # :elength.  If you specify one of these but not the other then you will
40   # not get the missing part.  e.g., :flength => 10 alone will return only
41   # the first 10 charcters of the string.  This is the same as the standard
42   # truncate(s, :length => 10) helper.
43   #
44   # If a :length parameter is provided it will override any other lengths
45   # specified.
46   #
47   def bitruncate(options = {})
48     maxlength = options[:length] || 0
49     flength = options[:flength] || 0
50     elength = options[:elength] || 0
51     omission = options[:omission] || '...'
52 
53     if maxlength == 0 && flength == 0 && elength == 0
54       maxlength = 30
55     end
56     if maxlength != 0
57       flength = maxlength / 2
58       elength = maxlength / 2
59     end
60 
61     return self if length <= (flength + elength)
62 
63     front = ''
64     back = ''
65     if flength > 0
66       front = self[0..(flength - 1)]
67     end
68     if elength > 0
69       back = self[(length - elength)..(length)]
70     end
71 
72     front + omission + back
73   end
74 end