Array#collect is faster than Array#inject

Also add some more tests.
This commit is contained in:
Jacques Distler 2009-12-13 20:00:18 -06:00
parent 282515d907
commit 1613973929
2 changed files with 14 additions and 3 deletions

View file

@ -392,7 +392,8 @@ class WikiController < ApplicationController
def truncate(text, length = 30, truncate_string = '...')
return text if text.length <= length
len = length - truncate_string.length
text.split.inject('') do |t, word|
t = ''
text.split.collect do | word|
if t.length + word.length <= len
t << word + ' '
else