From: Robert Klemme Date: 2010-11-24T00:38:04+09:00 Subject: Re: gsub and backslashes On Tue, Nov 23, 2010 at 12:39 PM, Ammar Ali wrote: > On Tue, Nov 23, 2010 at 11:17 AM, Robert Klemme > wrote: > >> On Mon, Nov 22, 2010 at 10:06 PM, Ammar Ali wrote: >> I agree, but this long "heritage" that goes back to the 60s is >> > probably very hard to shake. Maybe a new language can break away from >> > it. >> >> In Ruby's case the heritage does not go back to the sixties but rather >> to the nineties (1997) if I am not mistaken. > > I was thinking of C, which I believe introduced these escapes, but I'm not > sure. Yeah, but I don't want to change \n, \t etc. in double quoted strings. I mostly want to get rid of '\1' which is something completely specific to Ruby. >> > Out of curiosity, what could these beasts be replaced with? Constants? >> >> I'd leave everything as is except drop special cases like '\1' (this >> would either be an octal escape as in a double quoted string or rather >> just "1").  In single quoted strings only ' would be special if >> preceded by a backslash.  In double quoted strings I would have those >> characters which are special currently (", n, r, a, t and probably >> others I'm not thinking of right now).  I am undecided whether I would >> make all others errors or tolerant (e.g. "\z" would either by a syntax >> error or just "z").  I have a slight tendency to the more strict >> variant though because otherwise people might be left wondering what >> \z means when it is just "z"; also, this would help detect typing >> errors (maybe someone wanted to type "\t" which is just a key away in >> my German keyboard). > > I like the idea of treating unnecessary escapes as syntax errors, or at > least warnings. I see this a lot in regular expressions, especially in > character sets. Characters that don't need to be escaped (like ? and *) are > preceded with a backslash, just to be safe I guess, making for a harder to > code, as you noted. Exactly. I would not want to get rid of optional brackets for example because lack of brackets can make code much more readable (apart from foo.bar=(123) looking weird). It's always a question of balance. I have to say that Matz did a remarkable job at this in Ruby in general. This is just one of very few things that could be better (class variables is another one I can think of right now). Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/