I need read info from a file, extract the info that I want which is the
date and IP and dump them into a database (mysql).
However, this file gets constantly updated, sits on /var/log/.
Until now I have used just the file.open and file.read methods. However,
I feel that both ways are not suitable for what I want to do, because I
will need to make a selection between old and new entries every time my
script reads the file to extract info.
What is the standard (or best) way to manage this situation?
Here is the class which reads the file:
class ReadIPs
def self.ip(log)
file = File.open(log)
file.each_line do |line|
if line.include?(“Ban”)
a = line.split(" ")
date = a[0]
ip = a[6]
puts “date: #{date} IP: #{ip}”
end
end
file.close
end
end
I use File.open now but I should use something like File.append or
similar? Note that I will never write to this file, it’s just the file
that contains the info. Every time a new line appears that contains the
word “Ban” I want to trigger this…
class ReadIPs
def self.ip(log)
file = File.open(log)
Ha! There you are not using block form of File.open and you do not have
the #close in ensure!
end
I use File.open now but I should use something like File.append or similar? Note that I will never write to this file, it’s just the file that contains the info. Every time a new line appears that contains the word “Ban” I want to trigger this…
Best regards
I can’t test it right now but this might be an option.
File.open log do |io|
loop do
io.each do |line|
# whatever…
end
end
end
v1: seek to offset 0 of current position
v2: fetch the position before the sleep and then seek to that absolute position
Oh, this solutions, might work but I think that it’s a bit overcomplicated for me. I did not used IO and “seek” yet…
#seek is not used in the simplest case. What about reading up on #seek
and #tell or trying out the code? I don’t see where this is
complicated. If that’s too complicated for you then I either my
explanation made it look more complicated that it actually is or you
should abandon software development. You’ll encounter far more
complicated things in the course of it.
I found online [1] a few examples but I did not had the time to test them.
I am not sure whether pleac is a proper source to enter the world of
ruby. AFAIK their aim is to compare languages - not to present an
introductory read of particular languages. There are quite a few
tutorials and books out there for Ruby.
On 13 Δεκ 2009, at 4:35 μ.μ., Robert K. wrote:
Until now I have used just the file.open and file.read methods. However, I feel that both ways are not suitable for what I want to do, because I will need to make a selection between old and new entries every time my script reads the file to extract info.
v1: seek to offset 0 of current position
v2: fetch the position before the sleep and then seek to that absolute position
Oh, this solutions, might work but I think that it’s a bit overcomplicated for me. I did not used IO and “seek” yet…
#seek is not used in the simplest case. What about reading up on #seek and #tell or trying out the code? I don’t see where this is complicated. If that’s too complicated for you then I either my explanation made it look more complicated that it actually is or you should abandon software development. You’ll encounter far more complicated things in the course of it.
Given the fact that I’m messing with Ruby less than 8 days, I’ll pass
that note.
By complicated I meant that, ruby might have a build-in function or gem
that already deals with this situation of monitoring etc.
I found online [1] a few examples but I did not had the time to test them.
Maybe something like: file = File.open(path, File::WRONLY|File::APPEND)
Will do.
I am not sure whether pleac is a proper source to enter the world of ruby. AFAIK their aim is to compare languages - not to present an introductory read of particular languages. There are quite a few tutorials and books out there for Ruby.