Methods
Constants
Win32Exts = %w{.exe .com .bat}
LINKING_SUPPORTED = [true]
Win32Exts = %w{.exe .com .bat}
Public Instance methods
head(filename,lines=10) {|| ...}

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)).

  # Returns first 10 lines of 'myfile'
  FileUtils.head("myfile")
# File lib/facets/core/fileutils/slice.rb, line 14
  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
safe_ln(*args)

Attempt to do a normal file link, but fall back to a copy if the link fails.

# File lib/facets/core/fileutils/safe_ln.rb, line 15
  def safe_ln(*args)
    unless LINKING_SUPPORTED[0]
      cp(*args)
    else
      begin
        ln(*args)
      rescue Errno::EOPNOTSUPP
        LINKING_SUPPORTED[0] = false
        cp(*args)
      end
    end
  end
slice(filename,from,to) {|| ...}

In block form, yields lines ((from))-((to)). In non-block form, returns an array of lines ((from))-((to)).

  # Returns lines 8-12 of 'myfile'
  FileUtils.body("myfile",8,12)
# File lib/facets/core/fileutils/slice.rb, line 52
  def slice(filename,from,to) #:yield:
    IO.readlines(filename)[from-1..to-1]
  end
split_all(path)

Splits a file path into an array of individual path components. This differs from File.split, which divides the path into only two parts, directory path and basename.

  split_all("a/b/c") =>  ['a', 'b', 'c']
# File lib/facets/core/fileutils/split_all.rb, line 12
  def split_all(path)
    head, tail = File.split(path)
    return [tail] if head == '.' || tail == '/'
    return [head, tail] if head == '/'
    return split_all(head) + [tail]
  end
tail(filename,lines=10)

In block form, yields the last number of ((lines)) of file ((filename)). In non-block form, it returns the lines as an array.

Note that this method slurps the entire file, so I don’t recommend it for very large files. If you want an advanced form of ((tail)), I suggest using file-tail, by Florian Frank (available on the RAA). And no tail -f.

  # Returns last 3 lines of 'myfile'
  FileUtils.tail("myfile",3)
# File lib/facets/core/fileutils/slice.rb, line 39
  def tail(filename,lines=10) #:yield
    IO.readlines(filename).reverse[0..lines-1].reverse
  end
wc(filename,option='all')

With no arguments, returns a four element array consisting of the number of bytes, characters, words and lines in filename, respectively.

Valid options are bytes, characters (or just ‘chars’), words and lines.

  # Return the number of words in 'myfile'
  FileUtils.wc("myfile",'words')
# File lib/facets/core/fileutils/wc.rb, line 19
  def wc(filename,option='all')
    option.downcase!
    valid = %w/all bytes characters chars lines words/

    unless valid.include?(option)
        raise "Invalid option: '#{option}'"
    end

    n = 0
    if option == 'lines'
        IO.foreach(filename){ n += 1 }
        return n
    elsif option == 'bytes'
        File.open(filename){ |f|
          f.each_byte{ n += 1 }
        }
        return n
    elsif option == 'characters' || option == 'chars'
        File.open(filename){ |f|
          while f.getc
              n += 1
          end
        }
        return n
    elsif option == 'words'
        IO.foreach(filename){ |line|
          n += line.split.length
        }
        return n
    else
        bytes,chars,lines,words = 0,0,0,0
        IO.foreach(filename){ |line|
          lines += 1
          words += line.split.length
          chars += line.split('').length
        }
        File.open(filename){ |f|
          while f.getc
              bytes += 1
          end
        }
        return [bytes,chars,words,lines]
    end
  end
whereis(prog, path=ENV['PATH']) {|| ...}

In block form, yields each ((program)) within ((path)). In non-block form, returns an array of each ((program)) within ((path)). Returns (({nil})) if not found.

On the MS Windows platform, it looks for executables ending with .exe, .bat and .com, which you may optionally include in the program name.

   File.whereis("ruby") -> ['/usr/local/bin/ruby','/opt/bin/ruby']
# File lib/facets/core/fileutils/whereis.rb, line 25
  def whereis(prog, path=ENV['PATH']) #:yield:
    dirs = []
    path.split(File::PATH_SEPARATOR).each{|dir|
        # Windows checks against specific extensions
        if File::ALT_SEPARATOR
          if prog.include?('.')
              f = File.join(dir,prog)
              if File.executable?(f) && !File.directory?(f)
                if block_given?
                    yield f.gsub(/\//,'\\')
                else
                    dirs << f.gsub(/\//,'\\')
                end
              end
          else
              Win32Exts.find_all{|ext|
                f = File.join(dir,prog+ext)
                if File.executable?(f) && !File.directory?(f)
                    if block_given?
                      yield f.gsub(/\//,'\\')
                    else
                      dirs << f.gsub(/\//,'\\')
                    end
                end
              }
          end
        else
          f = File.join(dir,prog)
          # Avoid /usr/lib/ruby, for example
          if File.executable?(f) && !File.directory?(f)
              if block_given?
                yield f
              else
                dirs << f
              end
          end
        end
    }
    dirs.empty? ? nil : dirs
  end
which(prog, path=ENV['PATH'])

Looks for the first occurrence of program within path.

On the MS Windows platform, it looks for executables ending with .exe, .bat and .com, which you may optionally include in the program name. Returns nil if not found.

# File lib/facets/core/fileutils/which.rb, line 28
  def which(prog, path=ENV['PATH'])
    path.split(File::PATH_SEPARATOR).each {|dir|
      # Windows checks against specific extensions
      if File::ALT_SEPARATOR
        ext = Win32Exts.find{|ext|
          if prog.include?('.') # Assume extension already included
            f = File.join(dir,prog)
          else
            f = File.join(dir,prog+ext)
          end
          File.executable?(f) && !File.directory?(f) 
        }
        if ext
          # Use backslashes, not forward slashes
          if prog.include?('.') # Assume extension already included
            f = File.join( dir, prog ).gsub(/\//,'\\')
          else
            f = File.join( dir, prog + ext ).gsub(/\//,'\\')
          end
          return f
        end
      else
        f = File.join(dir,prog)
        # Avoid /usr/lib/ruby, for example
        if File.executable?(f) && !File.directory?(f)
          return File::join( dir, prog )
        end
      end
    }
    nil
  end