cpjolicoeur (owner)

Revisions

gist: 103523 Download_button fork
public
Public Clone URL: git://gist.github.com/103523.git
Embed All Files: show embed
Ruby regexp issue w/gsub.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
## The pattern matcher in both the following will match either the single or double quote
## at the start of the src= attribute (this is what is available in $1
 
## this regexp replacement works fine and will convert
## src="/foo/bar.png"
## to
## src="/RAILS_ROOT/public/foo/bar.png"
 
html_string.gsub!( /src=(["'])[\.\/]/ ) { |m| 'src=' + $1 + "#{RAILS_ROOT}/public/" }
    
## this regexp errors with the following error message
## TypeError: can't convert nil into String
## from (irb):84:in `+'
## from (irb):84
## from (irb):84:in `gsub'
## from (irb):84
 
html_string.gsub!( /src=(["'])\S+\?\d*\1/ ) { |s| s.split('?').first + $1 }
 
## the question is why can I do string concat with + $1 in the first regexp and
## why am I unable to do it in the second regexp where doing str + $1 throws the error??