Eli Green <eli.green / codedogs.ca> writes:

>> In fact, it is not the length or the '\\\\\\\\' strings needed and I
>> know that there are some work-arounds.
>> Still I think that it is a PITFALL: naturally when trying to replace \
>> by \\, knowing that '\' are escaped characters, you would write:
>> gsub(/\\/,'\\\\').
>> 
>> The need to either write gsub(/\\/,'\\\\\\\\') or use a work-around
>> gsub(/\\/){'\\\\'}, indicates (for me) than something is broken..
> 
> That seems harsh. =)
> 
> The only problem is that you're dealing with two parsers that happen to use
> the same character to do the same thing. It's a side-effect of the
> pure-OO'ness, and the truth is that if the ruby parser were to compensate for
> this special case, you'd be bastardizing the OO, since the parser shouldn't
> care where the string is headed.

Well ... it's also possible to have a consistent, totally O-O parser
that handles this case without any problem.  It just requires another
string representation.  For example, in Python, there's an alternative
string representation that looks like this:

  r'a string with a single \ in it'

The fact that it begins with "r'" tells Python's parser that
everything until the final single quote character is to be taken
literally, and therefore, backslashes don't need to be escaped.  The
only exception is when you want a single-quote in the string, in
which case you need to double it, as so:

  r'a string with a single '' in it'

The main place I see this usage in Python is in strings that are used
as arguments to the regular-expression parser, which is exactly what
we're talking about here.  And since the one character that we'd have
to double (single-quote) is not a regular-expression meta-character,
we don't get any quadrupling of characters.

I'm not trying to make any Ruby-Python comparisons here.  I only am
bringing this up to give a widely used, real-world example of a
rigidly O-O method of parsing strings that doesn't complicate the
parsing of regular expressions.


> [ ... ]


-- 
 Lloyd Zusman
 ljz / asfast.com