It is really hard to think about a real world project in which you are not going to have strings. As a result, having pre-existing methods for manipulating strings is really productive, as it saves you time. In today’s article, I want to emphasize a particular method in Ruby on Rails, that’s surprisingly not that much well known, which helps you with the whitespaces.
This method is squish and it handles both both ASCII and Unicode whitespace.
We can see it in action with a simple example:
As you can see, it really saves you time and effort from having to go and refer to a regex manual and treat every possible whitespace in a string with just a simple method call.
Its implementation is like the following implementation:
If you want to include this method inside a non Rails project, you can define it and use it yourself (assuming that you do not want to use ! symbol in the method’s name):
I hope you find this method beneficial and saving you from the headache of going all through the pain of implementing a similar method on your own.