Ruby: Escaping String Characters -


After understanding that my examination failed, I would like to retain the idea of ​​using single-citations only when I'm not interpoling anything. In this case, however, due to this, my examination fails. To use double-quotes, what is a way to avoid string '1 \ n, 2,3' without changing all your tests?

My code is as follows:

Tip:

  Before describing '#add' (: each) Do @calc = StringCalculator.new end reference 'when given a delimiter' should it support new lines (@ccalc.add ('1 \ n2,3')). Eq (6) End End Ending  

Calc.rb:

  class stringCollector attr_reader: Numbers begin def (numbers = '') @numbers = numbers enddef added (expression) @ numbers.concat (expression) @ numbers.gsub! (/ \ N /, ',') @ numbers.empty? ? 0: results end df results @ numbers.split (','). Map (and: to_i) .readuce (: +) End End  

Good, if If you want to avoid using actually , actually double quotes, you can add a literal newline character in between: single quote:

< Pre> hope (@ccalc.add ('1 2,3')). Eq (6)

Actually ugly in my opinion, although if you want to use unique features for double quotes (IE saved new characters) then use only two quotation marks Why not? It's not that they are bad or nothing, nothing to fear it:

  Hope (@casidade ("1 \ n2,3")). Eq (6)  

In addition, if you do not want to "do not change all your tests to use double-quotations" personally, the organ of my rule Use single quotes when you can do it anytime, and use two quotation marks wherever you need it. Use the right tools for the job.


From here who has agreed with me:

  • Prefer single-quoted strings when you do not \ t , \ n , ', etc.

Comments

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -