I have been away from ruby for awhile and I noticed something very strange (at least to me) in 1.9.3. Perhaps someone can explain it to me.
I was trying to split a string into lines so i did string.split('\n'), but this was giving me hell.
Eventually I tracked down the problem to using single quotes instead of double quotes. That is string.split("\n")
In the process of tracking this down I noticed a few things
'\n'.ord == 92"\n".ord == 10'\'.ordis not valid ruby'\\'.ord == 92
The only theory I have is that the single quotes cause ruby to not parse the string and thus treat \n as two characters. However, if this is the case why does '\' not pass the processor?
Am I missing something? Why doesn’t split convert the string to the correct ascii?
P.S. Here is some test code to illustrate my point
"asdf\nasdf".split('\n').size #=> 1
"asdf\nasdf".split("\n").size #=> 2
In addition to xdazz’s information,
'\'.ordis syntactically incorrect because the backslash escapes the following single quote. Use'\\'.ordfor that purpose instead.