Question

There is something mysterious to me about the escape status of a backslash within a single quoted string literal as argument of String#tr. Can you explain the contrast between the three examples below? I particularly do not understand the second one. To avoid complication, I am using 'd' here, which does not change the meaning when escaped in double quotation ("\d" = "d").

'\\'.tr('\\', 'x')      #=> "x"
'\\'.tr('\\d', 'x')     #=> "\\"
'\\'.tr('\\\d', 'x')    #=> "x"

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top