Sha256: 16381539beb725dbfa36bf2a192a2064c15cab153427cccdd89b4c05afca1c6f

Contents?: true

Size: 1.39 KB

Versions: 7

Compression:

Stored size: 1.39 KB

Contents

=begin
    Copyright 2010-2013 Tasos Laskos <tasos.laskos@gmail.com>

    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.
=end

class IO
    TAIL_BUF_LENGTH = 1 << 16

    #
    # @param    [Integer]   n
    #   Amount of lines to return from the bottom of the file.
    #
    # @return   [Array<String>]
    #   `n` amount of lines from the bottom of the file.
    #
    # @see http://stackoverflow.com/questions/3024372/how-to-read-a-file-from-bottom-to-top-in-ruby/3024704#3024704
    #
    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
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
arachni-0.4.5.2 lib/arachni/ruby/io.rb
arachni-0.4.5.1 lib/arachni/ruby/io.rb
arachni-0.4.5 lib/arachni/ruby/io.rb
arachni-0.4.4 lib/arachni/ruby/io.rb
arachni-0.4.3.2 lib/arachni/ruby/io.rb
arachni-0.4.3.1 lib/arachni/ruby/io.rb
arachni-0.4.3 lib/arachni/ruby/io.rb