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/more/facets/fileutils/slice.rb, line 11 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
Attempt to do a normal file link, but fall back to a copy if the link fails.
CREDIT Jim Weirich
# File lib/more/facets/fileutils/safe_ln.rb, line 14 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
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.
# File lib/more/facets/fileutils/slice.rb, line 48 def slice(filename,from,to) #:yield: IO.readlines(filename)[from-1..to-1] 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)
# File lib/more/facets/fileutils/slice.rb, line 36 def tail(filename,lines=10) #:yield IO.readlines(filename).reverse[0..lines-1].reverse 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
# File lib/more/facets/fileutils/wc.rb, line 16 def wc(filename,option='all') option.downcase! valid = %wall 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
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.
FileUtils.whereis("ruby") -> ['/usr/local/bin/ruby','/opt/bin/ruby']
CREDIT Daniel J. Berger
# File lib/more/facets/fileutils/whereis.rb, line 23 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
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
# File lib/more/facets/fileutils/which.rb, line 24 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