Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #11123: fast_banged_squish.diff

File fast_banged_squish.diff, 0.6 kB (added by jordi, 7 months ago)

Faster version of String#squish!

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

    old new  
    1111        #      string }.squish                   # => "Multi-line string" 
    1212        #   " foo   bar    \n   \t   boo".squish # => "foo bar boo" 
    1313        def squish 
    14           strip.gsub(/\s+/, ' ') 
     14          dup.squish! 
    1515        end 
    1616 
    1717        # Performs a destructive squish. See String#squish. 
    1818        def squish! 
    19           replace(squish) 
     19          strip! 
     20          gsub!(/\s+/, ' ') 
     21          self 
    2022        end 
    2123      end 
    2224    end