you are here: codestackercodes [RSS] → tag tail [RSS]

tail a file with ruby

def tail(filename, lines=10)
  buffer = []
  print_buffer = ''

  File.open(filename, 'r') do |f1|  
    while line = f1.gets  
      buffer << line
    end  
  end

  unless buffer.empty?
    p1 = 0
    p2 = buffer.length
    p1 = p2 - 10 if p2 > 10
    print_buffer = buffer[p1..p2].join 
  end

  print_buffer
end

puts tail('file.txt')
Displaying 1 Code