DEV Community 👩‍💻👨‍💻

Ahmad khattab
Ahmad khattab

Posted on

Discoveries in Ruby(and Rails): String#squish

How many times have you had a string that has some extra whitespace in the start/mid/end of a string. To remove them you would have to use strip that would successfully remove the whitespace at the end of a string. Let's see

"   Hello,    World     ".strip
 => "Hello,    World" 

Enter fullscreen mode Exit fullscreen mode

It has successfully removed the whitespaces at the start and end. But what about the middle ones?. Usually you would have to write a regex for that

"   Hello,    World     ".strip.gsub(/[[:space:]]+/, " ")
 => "Hello, World" 
Enter fullscreen mode Exit fullscreen mode

When using Rails. This method is available as String#squish
which does the same thing!

"Hello,      World    ".squish
 => "Hello, World" 
Enter fullscreen mode Exit fullscreen mode

Resources

Top comments (0)

Classic DEV Post

brain computer interface

Experimenting with brain-computer interfaces in JavaScript