Ruby Regex to update various variables inside a file

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
3
down vote

favorite












In our applications we have a version file that has three variables that are then joined to create a string that can be used for the semantic version.



The file looks like:



# frozen_string_literal: true

major = 0
minor = 1
patch = 0

PORTAL_VERSION = [major, minor, patch].join('.')


And then we update it using the following from our CI server:



@version = '0.1.1' # this is passed from somewhere

puts 'updating version'
version_file = File.open("version.rb").read
version = @version.split('.')
version_file = version_file.gsub(/(major = w)/, "major = #version[0]")
version_file = version_file.gsub(/(minor = w)/, "minor = #version[1]")
version_file = version_file.gsub(/(patch = w)/, "patch = #version[2]")
File.open('version.rb', 'wb') file
puts "updated version to #@version"


Is there a better way to do the replacement using the regex? As by having to find each line and then replace it feels a little dirty, plus if there was or wasn't spaces between the variable name and value it wouldn't work. So it feels a little fragile.







share|improve this question





















  • Why not generate a new version.rb from scratch instead of trying to edit it?
    – 200_success
    May 8 at 20:44
















up vote
3
down vote

favorite












In our applications we have a version file that has three variables that are then joined to create a string that can be used for the semantic version.



The file looks like:



# frozen_string_literal: true

major = 0
minor = 1
patch = 0

PORTAL_VERSION = [major, minor, patch].join('.')


And then we update it using the following from our CI server:



@version = '0.1.1' # this is passed from somewhere

puts 'updating version'
version_file = File.open("version.rb").read
version = @version.split('.')
version_file = version_file.gsub(/(major = w)/, "major = #version[0]")
version_file = version_file.gsub(/(minor = w)/, "minor = #version[1]")
version_file = version_file.gsub(/(patch = w)/, "patch = #version[2]")
File.open('version.rb', 'wb') file
puts "updated version to #@version"


Is there a better way to do the replacement using the regex? As by having to find each line and then replace it feels a little dirty, plus if there was or wasn't spaces between the variable name and value it wouldn't work. So it feels a little fragile.







share|improve this question





















  • Why not generate a new version.rb from scratch instead of trying to edit it?
    – 200_success
    May 8 at 20:44












up vote
3
down vote

favorite









up vote
3
down vote

favorite











In our applications we have a version file that has three variables that are then joined to create a string that can be used for the semantic version.



The file looks like:



# frozen_string_literal: true

major = 0
minor = 1
patch = 0

PORTAL_VERSION = [major, minor, patch].join('.')


And then we update it using the following from our CI server:



@version = '0.1.1' # this is passed from somewhere

puts 'updating version'
version_file = File.open("version.rb").read
version = @version.split('.')
version_file = version_file.gsub(/(major = w)/, "major = #version[0]")
version_file = version_file.gsub(/(minor = w)/, "minor = #version[1]")
version_file = version_file.gsub(/(patch = w)/, "patch = #version[2]")
File.open('version.rb', 'wb') file
puts "updated version to #@version"


Is there a better way to do the replacement using the regex? As by having to find each line and then replace it feels a little dirty, plus if there was or wasn't spaces between the variable name and value it wouldn't work. So it feels a little fragile.







share|improve this question













In our applications we have a version file that has three variables that are then joined to create a string that can be used for the semantic version.



The file looks like:



# frozen_string_literal: true

major = 0
minor = 1
patch = 0

PORTAL_VERSION = [major, minor, patch].join('.')


And then we update it using the following from our CI server:



@version = '0.1.1' # this is passed from somewhere

puts 'updating version'
version_file = File.open("version.rb").read
version = @version.split('.')
version_file = version_file.gsub(/(major = w)/, "major = #version[0]")
version_file = version_file.gsub(/(minor = w)/, "minor = #version[1]")
version_file = version_file.gsub(/(patch = w)/, "patch = #version[2]")
File.open('version.rb', 'wb') file
puts "updated version to #@version"


Is there a better way to do the replacement using the regex? As by having to find each line and then replace it feels a little dirty, plus if there was or wasn't spaces between the variable name and value it wouldn't work. So it feels a little fragile.









share|improve this question












share|improve this question




share|improve this question








edited May 8 at 14:51
























asked May 8 at 14:40









Cameron

215110




215110











  • Why not generate a new version.rb from scratch instead of trying to edit it?
    – 200_success
    May 8 at 20:44
















  • Why not generate a new version.rb from scratch instead of trying to edit it?
    – 200_success
    May 8 at 20:44















Why not generate a new version.rb from scratch instead of trying to edit it?
– 200_success
May 8 at 20:44




Why not generate a new version.rb from scratch instead of trying to edit it?
– 200_success
May 8 at 20:44










1 Answer
1






active

oldest

votes

















up vote
2
down vote













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(&: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 /(patchs*=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!(/(#keys*=s*w+)/, "#key = #version[i]")
    end


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






share|improve this answer





















    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "196"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f193928%2fruby-regex-to-update-various-variables-inside-a-file%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote













    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(&: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 /(patchs*=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!(/(#keys*=s*w+)/, "#key = #version[i]")
      end


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






    share|improve this answer

























      up vote
      2
      down vote













      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(&: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 /(patchs*=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!(/(#keys*=s*w+)/, "#key = #version[i]")
        end


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






      share|improve this answer























        up vote
        2
        down vote










        up vote
        2
        down vote









        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(&: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 /(patchs*=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!(/(#keys*=s*w+)/, "#key = #version[i]")
          end


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






        share|improve this answer













        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(&: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 /(patchs*=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!(/(#keys*=s*w+)/, "#key = #version[i]")
          end


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







        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered May 8 at 23:50









        Marc Rohloff

        2,56935




        2,56935






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f193928%2fruby-regex-to-update-various-variables-inside-a-file%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Chat program with C++ and SFML

            Function to Return a JSON Like Objects Using VBA Collections and Arrays

            Will my employers contract hold up in court?