Class: IO

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

Overview

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

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

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:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/arachni/ruby/io.rb', line 29

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