Sha256: f98a321a151bc93165a2adb25bca879ceaa0b4e6de70cf4e868792a9470b9c61

Contents?: true

Size: 612 Bytes

Versions: 3

Compression:

Stored size: 612 Bytes

Contents

require 'fileutils'
module FileUtils
  module_function
  # In block form, yields the first number of ((*lines*)) of file ((*filename*)).
  # In non-block form, it returns an array of the first number of ((*lines*)).
  #
  #   require 'facet/fileutils/head&tail'
  #
  #   # Returns first 10 lines of 'myfile'
  #   FileUtils.head("myfile")
  #  
  def head(filename,lines=10) #:yield:
    a = []
    IO.foreach(filename){|line|
        break if lines <= 0
        lines -= 1
        if block_given?
          yield line
        else
          a << line
        end
    }
    return a.empty? ? nil : a
  end 
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
facets-0.7.0 lib/facet/fileutils/head.rb
facets-0.7.1 lib/facet/fileutils/head.rb
facets-0.7.2 lib/facet/fileutils/head.rb