André Roaldseth
André Roaldseth

Reputation: 550

How can I use \w in double quoted string in Ruby

I'm using Capistrano for deployment and I need to run a command for setting the correct environment in the .htaccess file on the server.

I do that with sed like this: run "sed -i -r 's/APPLICATION_ENV \w+/APPLICATION_ENV #{stage}/' #{current_release}/public/.htaccess"

My problem is that the \w+ is escaped by Ruby/Cap and the regexp ends up trying to match only w+. If I use \\w+ I end up with a regexp that attempts to match \\w+.

How can I have an interpolated double quoted string and successfully escape the \w? Do I really need to change to concatenation of single quoted string and variables?

Upvotes: 2

Views: 112

Answers (1)

peter
peter

Reputation: 42192

On my sysem your sample works but you could try it with one of these two (comment the ones out you don't use)

begin
  stage = "stage"
  current_release = "current_release"
  s = "sed -i -r 's/APPLICATION_ENV \\w+/APPLICATION_ENV #{stage}/' #{current_release}/public/.htaccess"
  s = "sed -i -r 's/APPLICATION_ENV #{"\\w+"}/APPLICATION_ENV #{stage}/' #{current_release}/public/.htaccess"
  s = "sed -i -r 's/APPLICATION_ENV #{92.chr}w+/APPLICATION_ENV #{stage}/' #{current_release}/public/.htaccess"
  puts s
  system s
rescue 
  puts $! 
  system('pause') 
end 

Upvotes: 1

Related Questions