From: Luis Parravicini Date: 2007-05-31T00:16:36+09:00 Subject: Re: Substituting Variables On 5/30/07, Andrew Stewart wrote: > I am running into difficulties trying to use a variable in a > substitution. Here's an example: > > var = 'October' > '30 May 2007'.sub( /(\d+)\s(\w+)\s(\d+)/, '\1 #{var} \3' ) > > Here I would like to get '30 October 2007'. The back-references, > i.e. \1 and \3, only work within single quotation marks. But the > interpolation only works within double quotation marks. What to do? Use double slashes. irb(main):001:0> var = 'October' irb(main):002:0> '30 May 2007'.sub( /(\d+)\s(\w+)\s(\d+)/, "\\1 #{var} \\3" ) => "30 October 2007" -- Luis Parravicini http://ktulu.com.ar/blog/