Search code examples
rubycode-coverage

Is there any way to increment a number each time the file gets executed in ruby


Is there any way to increment a no. each time the file gets executed in ruby. I didn't find anything in this matter. I want to declare a no. and then it will be incremented each time the file in which it resides gets executed. I can use file to keep track of last no. used but is there any simple solution


Solution

  • # 0 TIMES
    def  get_no
      me = File.read(__FILE__)
      me.sub!(/ \d+ TIMES/) { |s| " #{s[/\d+/].to_i + 1} TIMES" }
      l=me.split("\n")
      File.open(__FILE__,'w') { |f| f.write(me) }
      return l[0][/\d+/]
    end
    

    This method will give me next no each time I call it and also will keep track of it thanks to Alex D