I am trying to use sting substitution in a ruby regex that spans over multiple lines. I believe the problem is that in Free-Spacing mode the '#' is seen as a comment.
First of all is there a better way to breakup long regexes and secondly, how should i perform substitutions in Free-Spacing regexes
The code below has 2 examples. get_module_name_a with the regex all on one line that works fine and the get_module_name_b with the free-spacing regex where the substitution ends up being seen as a comment (I think).
Ideally I would like to keep the length of my lines below 80 chars.
The output of the code is currently
$ ruby test.rb
testmod2
test.rb:42:in `get_module_name_b': undefined method `[]' for nil:NilClass (NoMethodError)
from test.rb:46:in `<main>'
Sample code:
#!/usr/bin/env ruby
def loadFile
"
mod 'testmod1',
:git => '[email protected]:reaktor/testmod1.git',
:ref => 'RELEASE_1.0.0'
mod 'testmod2',
:git => '[email protected]:reaktor/myproject-testmod2.git',
:ref => 'RELEASE_2.0.10'
mod 'testmod3',
:git => '[email protected]:reaktor/testmod3.git',
:tag => 'RELEASE_10.2.3'
"
end
def get_module_name_a(repo_name)
input_string = loadFile
regex = /mod ["'](\w*)["'],\s*$\n+(\s*):git\s*=>\s*["'].*#{repo_name}.git["'],$\n+(\s*):ref\s*=>\s*['"](\w+|\w+\.\d+\.\d+)['"]$/
result = regex.match(input_string)
result[1]
end
def get_module_name_b(repo_name)
input_string = loadFile
regex = /\A
mod ["'](\w*)["'],\s*$\n
+(\s*):git\s*=>\s*["'].*#{repo_name}.git["'],$\n
+(\s*):ref\s*=>\s*['"](\w+|\w+\.\d+\.\d+)['"]$
\Z/x
result = regex.match(input_string)
result[1]
end
puts get_module_name_a('myproject-testmod2')
puts get_module_name_b('myproject-testmod2')