the12 the12 - 4 months ago 28
Ruby Question

Spaces in string vs. Escape sequences

In Ruby (testing in the Interactive Ruby Shell), you can either use spaces in strings or use escape sequences. Is there a best practice in determining which to use, or does it never matter?

For example:

"Hello World"

--> "Hello World"

vs.

"Hello\sWorld"

--> "Hello World"

Answer

Use spaces. Makes it much more easier to read

If you cant, then use the escape sequences.

Also note that escape only works in double quotes

   'Hello\sWolrd'
    --> 'Hello\\sWorld'