class String
def truncate length
if length > 0
self.strip!
if self.length > length
out_array = []
words = self.split(' ')
for word in words
if ((out_array + [word]).join(' ').length) < length
out_array << word
else
break
end
end
string_to_return = out_array.join(' ')
# in case truncate is too short
if string_to_return.length == 0
string_to_return = self[0..length]
end
# remove any trailing spaces.
string_to_return.strip!
# special last characters
if string_to_return[-1,1] == '.'
string_to_return += '..'
elsif string_to_return[-1,1] == ','
string_to_return.chop!
string_to_return += '...'
else
string_to_return += '...'
end
return string_to_return
else
return self
end
end
end
end
Sunday, April 03, 2011
String Truncate
Here's a truncate method that's a little smarter than the one that comes with rails. It won't cut off in the middle of a word. Just put this code in a file (like string_initializers.rb) and put that your initializers folder under config.
Subscribe to:
Post Comments (Atom)
Thank you! Used your script
ReplyDeleteThe built in 'truncate' method on String is worth noting.
ReplyDeletehttp://apidock.com/rails/String/truncate
This won't look for the nearest ' ', unfortunately, but is probably good enough...