Jesse Jones <jesjones / mindspring.com> writes:

> According to the pickaxe book (page 64), this:
> 
> text = "fred:smith"
> puts text.sub(/(\w+):(\w+)/, "\2, \1")
> 
> should print "smith:fred", but on my system it prints ", ".

Look closer. The pickaxe book example uses single quotes: '\2, \1'.
When you use double quotes, Ruby interprets the blackslash sequences
as character codes. Single quotes prevent interpolation of the
backslashes by Ruby, so they can be seen and interpreted correctly by
String#sub.

% ruby -e 'p "\2, \1".size'
4
% ruby -e "p '\2, \1'.size"
6

You can still use double quotes, but then you need to double the
backslashes: "\\2, \\1".

-- 
Pierre-Charles David (pcdavid <at> emn <dot> fr)
Computer Science PhD Student, ±Äole des Mines de Nantes, France
Homepage: http://purl.org/net/home/pcdavid