Ruby Regex to update various variables inside a file
There might be two easier options:


Generate the file using a template file something like the following 

major = {{major}}
minor = {{minor}}
patch = {{patch}}



that has strings that are easy to search for. You could also use ERB to generate the file.


Put the logic in your version.rb file something like:

PORTAL_VERSION = '1.2.3'
major, minor, path = PORTAL_VERSION.split('.').map(&amp;:to_i)



This way you would just have to replace the first line.


If you want to do it using your logic I would change the regex from /(patch = \w)/ to /(patch\s*=\s*\w+)/ this would handle extra whitespace and version numbers bigger than 9
Note you can also do variable substitution in Regexes so you could write your code as:

['major', 'minor', 'patch'].each.with_index do |key, i|
  version_file.sub!(/(#{key}\s*=\s*\w+)/, "#{key} = #{version[i]}")
end



Note that gsub isn't necessary as you only expect one occurrence.