def things_n_stuff

Adventures in Code

Today in 'Awesome Rails Tidbits: Warm and Squishy Version'

This morning I was scraping and parsing data and came across this ugly string that I needed to save to the db:

1
string = "\n\t\t\t\tOh my gosh check - (out this string) 75-3\n\n\t\t\t\t453rd Prm. (random, other, 8, *&^, 'it's ugly as heck')))\n\n\t\t\t\t(other random(nested things)) followed by 33, stuff\n\n\n\t\t\t\n"

I only needed the text and punctualtion from it, but none of the line-breaks or tabs and spacing.

My ‘just make it work’ first swing at it was this inelegant mashup of regex and method chaining, removing the formating characters:

1
2
> string.gsub(/[\n+\t+\s+]/,' ').split.join(' ').strip
#=> "Oh my gosh check - (out this string) 75-3 453rd Prm. (random, other, 8, *&^, 'it's ugly as heck'))) (other random(nested things)) followed by 33, stuff"

… and shortly thereafter I discovered Rails’ beautiful squish method. From rdoc:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
(from gem activesupport-4.0.0)
=== Implementation from String
------------------------------------------------------------------------------
  squish()

------------------------------------------------------------------------------

Returns the string, first removing all whitespace on both ends ofthe string,
and then changing remaining consecutive whitespacegroups into one space each.

Note that it handles both ASCII and Unicode whitespace like mongolian vowel
separator (U+180E).

  %{ Multi-line
     string }.squish                   # => "Multi-line string"
  " foo   bar    \n   \t   boo".squish # => "foo bar boo"

And like beautiful concise magic, voilà:

1
2
> string.squish
# => "Oh my gosh check - (out this string) 75-3 453rd Prm. (random, other, 8, *&^, 'it's ugly as heck'))) (other random(nested things)) followed by 33, stuff"

Clean and useful.