From: Robert Klemme Date: 2010-01-14T20:43:22+09:00 Subject: Re: Ruby Regex 2010/1/14 Brian Candler : > Robert Klemme wrote: >> For me there is actually something weird about Ruby's escape handling >> - but it's something else: in some circumstances Ruby allows you to >> *omit* a backslash which is meant to be convenient (I believe) but >> which leads to a certain inconsistency: >> >> irb(main):014:0> '\1'  # this might be seen as surprising >> => "\\1" > > I think the principle is "single quoting does the absolute minimum > amount of dequoting". Hmm, I never thought of it that way. I'm not sure I like this principle though. > However it *has* to support a way to get a single-quote within a > single-quoted string, and they chose \'. As a consequence, it *has* to > support \\ to get a single backslash within a single-quoted string. > > The question then is, should any other sequence like \1 raise an error, > or return literal \ and 1 ? I opt for raising a syntax error. I know, this is unlikely to happen anytime soon if only because of the large base of code that is potentially affected. With what I have seen over the past years, the number of backslashes needed for proper quoting (especially for #gsub and friends) has caused much confusion. I believe that could be avoided by disallowing the '\1'. > The alternative would have been to use two single quotes where you want > a single quote within a string: > >   'It''s that time of day' > > I quite like that, but arguably it's just confusing in a different way. I like the quoting approach better. :-) Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/