ptools, aka "power tools", is an additional set of commands for the 'File' class based on *nix command line tools. Version 0.1.2 includes: which, whereis, wc, tail, touch, nl_convert, middle, and head.
require "ptools" File.which("ruby") -> '/usr/local/bin/ruby' File.whereis("ruby") -> ['/usr/local/bin/ruby','/opt/bin/ruby'] File.head("myfile") # Returns first 10 lines of 'myfile' File.middle("myfile",8,12) # Returns lines 8-12 of 'myfile' File.tail("myfile",3) # Returns last 3 lines of 'myfile' File.wc("myfile",'words') # Returns the number of words in 'myfile' File.touch("newfile") # "newfile" now exists # Creates a copy of 'myfile' called 'newfile', in DOS format File.nl_convert("myfile","newfile","dos")
PTOOLS_VERSION
The current version of the ptools package, returned as a string.
File.head(filename,no_lines=10)
File.head(filename,no_lines=10){|line| ... }
In block form, yields the first no_lines of file filename. In non-block form, it returns an array of the first no_lines.
File.middle(fielname,from=10,to=20)
In block form, yields lines from-to. In non-block form, returns an array of lines from-to.
File.nl_convert(old_file, new_file=old_file, format="dos")
Converts a text file from one OS platform format to another, ala dos2unix. Valid values for format, which are case insensitve, include:
Note that this method is only valid for an ftype of "file". Otherwise a TypeError will be raised. If an invalid format value is received, StandardError is raised.
File.tail(filename,no_lines=10)
File.tail(filename,no_lines=10){|line| ... }
In block form, yields the last no_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).
File.touch(filename)
Creates an empty file with the name filename.
File.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.
File.which(program,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. Returns nil
if not found.
File.whereis(program,path=ENV['PATH'])
File.whereis(program,path=ENV['PATH']){|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.
There is a bug in 1.6.x that can cause $\ characters to accumulate when converting to DOS or MAC format if nl_convert is run multiple times on the same file. This appears to be fixed in 1.8.0.
The which() method was adopted from the FileWhich code posted by Michael Granger on http://www.rubygarden.org. The whereis() method is a minor modification of that code as well. The nl_convert() method was adopted (somewhat) from the nlcvt program found at http://www.perl.com/language/ppt/src/nlcvt/nlcvt, written by Tom Christiansen. The middle() method was provided by Shanko.
Add whatever other tools people think might be useful.
Ruby's
Daniel J. Berger djberg96 at yahoo dot com rubyhacker1 on IRC