From: Intransition Date: 2012-01-22T10:54:07+09:00 Subject: Re: [regexp] How to convert string "/regexp/i" to /regexp/i - ? ------=_Part_974_21414723.1327197244403 Content-Type: multipart/alternative; boundary="----=_Part_975_12425914.1327197244403" ------=_Part_975_12425914.1327197244403 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Something like: key = "ixm" x = str.reverse_index('/') v = str[x+1..-1] o = v.characters.inject(0){ |s,c| b = key.index(c); b ? 2**b : 0 } Regexp.new(str[1...x], o) Or eval "%r#{str}" If safety is a concern you can get Pretty Good(tm) safety by wrapping it in a threaded $SAFE=4. Too bad Ruby doesn't support %R, then it would perfectly safe. ------=_Part_975_12425914.1327197244403 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 7bit Something like:
  
   key = "ixm"

   x = str.reverse_index('/')

   v = str[x+1..-1]

   o = v.characters.inject(0){ |s,c| b = key.index(c); b ? 2**b : 0 }

   Regexp.new(str[1...x], o)

Or

  eval "%r#{str}"

If safety is a concern you can get Pretty Good(tm) safety by wrapping it in a threaded $SAFE=4. Too bad Ruby doesn't support %R, then it would perfectly safe.


------=_Part_975_12425914.1327197244403-- ------=_Part_974_21414723.1327197244403--