ror,如何使正则表达式动态化?

AnA*_*ice 1 ruby ruby-on-rails ruby-on-rails-3

鉴于这种:

if (params[:to].to_s =~ (/^r\+.*@site.com$/)) == nil
Run Code Online (Sandbox Code Playgroud)

如何使site.com动态: #{SITE_CONFIG['mail_host']}

我试过了

if (params[:to].to_s =~ (/^r\+.*@#{SITE_CONFIG['mail_host']}$/)) == nil
Run Code Online (Sandbox Code Playgroud)

哪个没用..想法?

How*_*ard 5

在ruby中,您可以使用以下命令从字符串表示创建正则表达式Regexp.new:

if (params[:to].to_s =~ Regexp.new("^r\\+.*@#{Regexp.quote(SITE_CONFIG['mail_host'])}$")) == nil
Run Code Online (Sandbox Code Playgroud)