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
if condition
# …
else
# …
end
not
if condition
# …
end
else
# …
end
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With