Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: else without rescue is useless

I'm new to ruby. I'm trying to write an apache error.log monitor. It is mostly done, but I'm getting the warning: else without rescue is useless. I can't figure out what I'm doing wrong. Does Ruby want me to use a 'unless'?

class ErrorMonitor
   @@previous_size=0
   @@counter=0

   def initialize()
   end

   def process
    if @@counter > 0
       @new_size= File.stat('/var/log/apache2/error.log').size
       if @new_size > @@previous_size
          for i in @@previous_size..@new_size - @@previous_size
             print IO.readlines("/var/log/apache2/error.log")[i]
          end
          @@previous_size = @new_size
       end
    end
    else
       @@previous_size= File.stat('/var/log/apache2/error.log').size
       @@counter=1;
    end # <- this line is where the warning points to
   end


# main execution
em = ErrorMonitor.new()
while true
    em.process
    sleep 10
end
like image 785
ed_is_my_name Avatar asked Feb 16 '14 22:02

ed_is_my_name


2 Answers

if condition
  # …
else
  # …
end

not

if condition
  # …
end
else
  # …
end
like image 194
DMKE Avatar answered Sep 23 '22 20:09

DMKE


It looks like the else block is not part of an if statement. Am I correct in assuming you want it to provide an alternative path when if @@counter > 0 is false? If so, get rid of the end that's on the line above the else, e.g.:

if @@Counter > 0
    # ... processing ...
else
    # ... alternative processing ...
end
like image 2
neuronaut Avatar answered Sep 24 '22 20:09

neuronaut