Class: IO

Inherits:
Object show all
Defined in:
lib/arachni/ruby/io.rb

Overview

Copyright 2010-2015 Tasos Laskos <[email protected]>

This file is part of the Arachni Framework project and is subject to
redistribution and commercial restrictions. Please see the Arachni Framework
web site for more information on licensing and terms of use.

Constant Summary collapse

TAIL_BUF_LENGTH =
1 << 16

Instance Method Summary collapse

Instance Method Details

#tail(n) ⇒ Array<String>

Returns ‘n` amount of lines from the bottom of the file.

Parameters:

  • n (Integer)

    Amount of lines to return from the bottom of the file.

Returns:

  • (Array<String>)

    ‘n` amount of lines from the bottom of the file.

See Also:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/arachni/ruby/io.rb', line 19

def tail( n )
    return [] if n < 1

    seek_to = TAIL_BUF_LENGTH
    seek_to = size if seek_to > size

    seek -seek_to, IO::SEEK_END

    buf = ''
    while buf.count( "\n" ) <= n
        buf = read( seek_to ) + buf

        seek_to *= 2
        seek_to = size if seek_to > size

        seek -seek_to, IO::SEEK_CUR
    end

    buf.split( "\n" )[-n..-1]
end