Friday, September 16, 2011

To Quote or Not Two Quotes, Taht Iz Dah Qvestion

In C, when one needed to insert a special character or ensure a regular character was interpreted as-is, one would use the backslash character (for those of you in Rio Linda, that's the '\', not the forward slash '/'), like so:


\n 
\" 
\\

In Ruby, supposedly you can do the same thing. The exception is that in Ruby, the action of the backslash is restricted within single-quoted strings. That is,

" this is a quote: \"" (length 18) 
' this is a quote: \'" (length 19)


Be mindful of your escaping, you could be escaping too much, depending on the type of quotes you chose. This often caused trouble in our RSpec tests, particularly when Capybara "have_content" matchers were involved.


Post a Comment