Search code examples
rubyredminepopen

Why does my code work in irb but not from a script?


I want to do some analysis of the Redmine log files, including the previous log. So I want to catenate logfile.log.0 and logfile.log, and loop through the output one line at a time. I wrote the following code:

module MyModule
   def analyze_log
     logfile = '/www/redmine/log/logfile.log'
     cat_cmd = "cat #{logfile}.0 #{logfile}"
     cat = IO.popen(cat_cmd)
     cat.readlines do |line|
        puts line
     end
  end
end

The code (without the module and method definition) works perfectly when I do it in irb, but on the same machine it does not work (does not print the lines) when I wrap the code in a method (analyze_log) in a module (MyModule), and call that from a script, as follows:

#!/opt/ruby/bin/ruby
require './my_module'
include MyModule
analyze_log

What gives?

BTW, if there is a better way to process multiple files sequentially in the same loop, I'd be happy to hear about it. But my main concern is that it works in irb but not when run as a script.

I AM running the script as the same user for which I ran irb.


Solution

  • A more Ruby way of doing this is to use internal functions to handle this:

    module MyModule
      def analyze_log
        logfile = '/www/redmine/log/logfile.log'
    
        [
          "#{logfile}.0",
          logfile
        ].each do |file|
          File.readlines(file).each do |line|
            print line
          end
        end
      end
    end
    

    Running a sub-process to read a file is completely unnecessary.