2

Why do the following strings give me the same output in the Ruby interpreter?

  'f:\new'
  'f:\\new'

Both strings result in: "f:\\new". I was expecting the second string to display "f:\\\\new" (if not that, then the first one should have shown "f:\new")

Agnel Kurian
  • 57,975
  • 43
  • 146
  • 217
  • I do not see the difference in your two input strings. Perhaps you mistyped. – murgatroid99 Jul 21 '10 at 13:33
  • I'm not familiar with Ruby but I guess the first is detected as an incorrect use of backslash (in a single-quote string, it should have been followed by backslash or single quote), so the backslash is preserved... – pascal Jul 21 '10 at 13:37
  • @murgatroid99, do you see the difference now? (There was an SO formatting error.) – Agnel Kurian Jul 21 '10 at 13:38
  • @murgatroid99 :- I edit the question now check it. @Vulcan Eager :- Please check if i correctly edit or not ? – Salil Jul 21 '10 at 13:38
  • 1
    possible duplicate of [Backslashes in Single quoted strings vs. Double quoted strings in Ruby?](http://stackoverflow.com/questions/648156/backslashes-in-single-quoted-strings-vs-double-quoted-strings-in-ruby). In particular, look at [this page](http://en.wikibooks.org/wiki/Ruby_Programming/Strings#Single%5Fquotes) that was linked in the accepted answer. – mikej Jul 21 '10 at 13:48
  • @mikej, Not the same question. The devil is in the details. – Agnel Kurian Jul 21 '10 at 13:54
  • @Salil, The edit shows the difference now – murgatroid99 Jul 21 '10 at 13:55
  • @Vulcan OK, sorry if I was a bit close-trigger happy :) looks like your question is a more specific version of the general question. The *reason* is the same as the one given in that question though: In a single quoted string \\ becomes \ and \' becomes '. Everything else is left alone. – mikej Jul 21 '10 at 14:01

1 Answers1

2

Single-quoted strings support only two escape sequences: \' and \\

That's why in your first example \n is not treated as new-line char: it's not in the list.

Nikita Rybak
  • 67,365
  • 22
  • 157
  • 181