From: mike@... (Mike Stok) Date: 2003-07-11T03:00:40+09:00 Subject: Re: Ruby T-Shirt Idea In article <035a01c346a5$7740f600$0300a8c0@austin.rr.com>, Hal E. Fulton wrote: [...] >BTW, var = Regexp.new(Regexp.escape(var)) will >also do that conversion... probably more than >two ways... ;) > >As for interpolating a variable into a regex, >I'm not sure whether it does automatic escaping >or not. I'd like to think it does, but there >may be some good reason it doesn't. Too lazy to >test right now... It doesn't automatically escape the interpolated thing and that's a good thing. Sometimes I construct alternations dynamically e.g. [mike@ratdog mike]$ irb --simple-prompt >> words = %w{foo bar baz} => ["foo", "bar", "baz"] >> match_a_word = /\b(?:#{words.join('|')})\b/ => /\b(?:foo|bar|baz)\b/ >> "April fool" =~ match_a_word => nil >> "this has a foo in it" =~ match_a_word => 11 and I don't want the interpolation to quietly "save me from myself." Of course it's my responsibility to escape things which need escaping. Sometimes I do miss a perl-like \Q in an interpolated string, but the power of #{ ... } more than makes up for it. Mike -- mike@stok.co.uk | The "`Stok' disclaimers" apply. http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA mike@exegenix.com | Fingerprint 0570 71CD 6790 7C28 3D60 http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA