A
Andrew Stewart
Hello,
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?
Here's an example in IRB:
=> "30 \#{var} 2007"
=> "\001 October \003"
=> "30 October 2007"
Actually that last one does what I want -- but according to the
Pickaxe[1] $1 and friends aren't supposed to work and in fact it
doesn't work in my Ruby class. So I think IRB is flattering to deceive.
[1] String.gsub: "If a string is used as the replacement, special
variables from the match (such as $& and $1) cannot be substituted
into it, as substitution into the string occurs before the pattern
match starts. However, the sequences \1, \2, and so on may be used
to interpolated successive groups in the match."
I have found a workaround but I'm hoping there's a better way:
=> "30 October 2007"
Any insight would be much appreciated.
Thanks and regards,
Andy Stewart
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?
Here's an example in IRB:
=> "30 \#{var} 2007"
=> "\001 October \003"
=> "30 October 2007"
Actually that last one does what I want -- but according to the
Pickaxe[1] $1 and friends aren't supposed to work and in fact it
doesn't work in my Ruby class. So I think IRB is flattering to deceive.
[1] String.gsub: "If a string is used as the replacement, special
variables from the match (such as $& and $1) cannot be substituted
into it, as substitution into the string occurs before the pattern
match starts. However, the sequences \1, \2, and so on may be used
to interpolated successive groups in the match."
I have found a workaround but I'm hoping there's a better way:
=> "30 October 2007"
Any insight would be much appreciated.
Thanks and regards,
Andy Stewart