Daniel Berger
2002-07-29 20:15:25 UTC
Hi all,
I've been playing with a Ruby implementation of the *nix 'tail' command. Here's
what I've come up with so far. I thought I'd put it out there for comment and see
what people think.
It doesn't do anything fancy - it's not nearly as smart as it's Perl equivalent yet,
for example. But, it works.
Requires 1.7.2 (for the sysseek method).
# While this is running, try doing
# "echo 'sometext' >> test.txt" once in a while
require 'tail'
t = Tail.new("test.txt") # Or whatever
# Ctrl-C to exit
while true
puts t.read
end
# Da code
class Tail
attr_accessor :file, :buffer, :interval
def initialize(file, buffer=8192, interval=10)
@file = file
@buffer = buffer
@interval = interval
@fh = File.open(file,"r")
@fh.sysseek(-1,2)
end
def read
begin
return @fh.sysread(buffer).tr("\n","")
rescue EOFError
@fh.sysseek(-1,2)
sleep interval
retry
end
end
end
I've been playing with a Ruby implementation of the *nix 'tail' command. Here's
what I've come up with so far. I thought I'd put it out there for comment and see
what people think.
It doesn't do anything fancy - it's not nearly as smart as it's Perl equivalent yet,
for example. But, it works.
Requires 1.7.2 (for the sysseek method).
# While this is running, try doing
# "echo 'sometext' >> test.txt" once in a while
require 'tail'
t = Tail.new("test.txt") # Or whatever
# Ctrl-C to exit
while true
puts t.read
end
# Da code
class Tail
attr_accessor :file, :buffer, :interval
def initialize(file, buffer=8192, interval=10)
@file = file
@buffer = buffer
@interval = interval
@fh = File.open(file,"r")
@fh.sysseek(-1,2)
end
def read
begin
return @fh.sysread(buffer).tr("\n","")
rescue EOFError
@fh.sysseek(-1,2)
sleep interval
retry
end
end
end