Module FileUtils
In: lib/more/facets/fileutils/slice.rb
lib/more/facets/fileutils/wc.rb
lib/more/facets/fileutils/which.rb
lib/more/facets/fileutils/whereis.rb
lib/more/facets/fileutils/safe_ln.rb

Methods

head   safe_ln   slice   tail   wc   whereis   which  

Constants

Win32Exts = %w{.exe .com .bat .cmd}
Win32Exts = %w{.exe .com .bat .cmd}
LINKING_SUPPORTED = [true]

Public Instance methods

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

[Source]

    # File lib/more/facets/fileutils/slice.rb, line 11
11:   def head(filename,lines=10) #:yield:
12:     a = []
13:     IO.foreach(filename){|line|
14:         break if lines <= 0
15:         lines -= 1
16:         if block_given?
17:           yield line
18:         else
19:           a << line
20:         end
21:     }
22:     return a.empty? ? nil : a
23:   end

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

CREDIT Jim Weirich

[Source]

    # File lib/more/facets/fileutils/safe_ln.rb, line 14
14:   def safe_ln(*args)
15:     unless LINKING_SUPPORTED[0]
16:       cp(*args)
17:     else
18:       begin
19:         ln(*args)
20:       rescue Errno::EOPNOTSUPP
21:         LINKING_SUPPORTED[0] = false
22:         cp(*args)
23:       end
24:     end
25:   end

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)

CREDIT Shashank Date, via Daniel Berger.

[Source]

    # File lib/more/facets/fileutils/slice.rb, line 48
48:   def slice(filename,from,to) #:yield:
49:     IO.readlines(filename)[from-1..to-1]
50:   end

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)

[Source]

    # File lib/more/facets/fileutils/slice.rb, line 36
36:   def tail(filename,lines=10) #:yield
37:     IO.readlines(filename).reverse[0..lines-1].reverse
38:   end

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

CREDIT Daniel J. Berger

[Source]

    # File lib/more/facets/fileutils/wc.rb, line 16
16:   def wc(filename,option='all')
17:     option.downcase!
18:     valid = %w/all bytes characters chars lines words/
19: 
20:     unless valid.include?(option)
21:         raise "Invalid option: '#{option}'"
22:     end
23: 
24:     n = 0
25:     if option == 'lines'
26:         IO.foreach(filename){ n += 1 }
27:         return n
28:     elsif option == 'bytes'
29:         File.open(filename){ |f|
30:           f.each_byte{ n += 1 }
31:         }
32:         return n
33:     elsif option == 'characters' || option == 'chars'
34:         File.open(filename){ |f|
35:           while f.getc
36:               n += 1
37:           end
38:         }
39:         return n
40:     elsif option == 'words'
41:         IO.foreach(filename){ |line|
42:           n += line.split.length
43:         }
44:         return n
45:     else
46:         bytes,chars,lines,words = 0,0,0,0
47:         IO.foreach(filename){ |line|
48:           lines += 1
49:           words += line.split.length
50:           chars += line.split('').length
51:         }
52:         File.open(filename){ |f|
53:           while f.getc
54:               bytes += 1
55:           end
56:         }
57:         return [bytes,chars,words,lines]
58:     end
59:   end

[Source]

    # File lib/more/facets/fileutils/whereis.rb, line 23
23:   def whereis(prog, path=ENV['PATH']) #:yield:
24:     dirs = []
25:     path.split(File::PATH_SEPARATOR).each{|dir|
26:         # Windows checks against specific extensions
27:         if File::ALT_SEPARATOR
28:           if prog.include?('.')
29:               f = File.join(dir,prog)
30:               if File.executable?(f) && !File.directory?(f)
31:                 if block_given?
32:                     yield f.gsub(/\//,'\\')
33:                 else
34:                     dirs << f.gsub(/\//,'\\')
35:                 end
36:               end
37:           else
38:               Win32Exts.find_all{|ext|
39:                 f = File.join(dir,prog+ext)
40:                 if File.executable?(f) && !File.directory?(f)
41:                     if block_given?
42:                       yield f.gsub(/\//,'\\')
43:                     else
44:                       dirs << f.gsub(/\//,'\\')
45:                     end
46:                 end
47:               }
48:           end
49:         else
50:           f = File.join(dir,prog)
51:           # Avoid /usr/lib/ruby, for example
52:           if File.executable?(f) && !File.directory?(f)
53:               if block_given?
54:                 yield f
55:               else
56:                 dirs << f
57:               end
58:           end
59:         end
60:     }
61:     dirs.empty? ? nil : dirs
62:   end

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.

CREDIT Daniel J. Berger & Michael Granger

[Source]

    # File lib/more/facets/fileutils/which.rb, line 24
24:   def which(prog, path=ENV['PATH'])
25:     path.split(File::PATH_SEPARATOR).each {|dir|
26:       # Windows checks against specific extensions
27:       if File::ALT_SEPARATOR
28:         ext = Win32Exts.find{|ext|
29:           if prog.include?('.') # Assume extension already included
30:             f = File.join(dir,prog)
31:           else
32:             f = File.join(dir,prog+ext)
33:           end
34:           File.executable?(f) && !File.directory?(f)
35:         }
36:         if ext
37:           # Use backslashes, not forward slashes
38:           if prog.include?('.') # Assume extension already included
39:             f = File.join( dir, prog ).gsub(/\//,'\\')
40:           else
41:             f = File.join( dir, prog + ext ).gsub(/\//,'\\')
42:           end
43:           return f
44:         end
45:       else
46:         f = File.join(dir,prog)
47:         # Avoid /usr/lib/ruby, for example
48:         if File.executable?(f) && !File.directory?(f)
49:           return File::join( dir, prog )
50:         end
51:       end
52:     }
53:     nil
54:   end

[Validate]