Class: File
- Inherits:
-
Object
- Object
- File
- Defined in:
- lib/ptools.rb
Constant Summary collapse
- PTOOLS_VERSION =
'1.1.3'
- IS_WINDOWS =
false
Class Method Summary collapse
-
.binary?(file) ⇒ Boolean
Returns whether or not
file
is a binary file. -
.head(filename, num_lines = 10) ⇒ Object
In block form, yields the first
num_lines
fromfilename
. -
.middle(filename, from = 10, to = 20) ⇒ Object
In block form, yields line
from
up to lineto
. -
.nl_convert(filename, newfilename = filename, platform = "dos") ⇒ Object
Converts a text file from one OS platform format to another, ala ‘dos2unix’.
-
.null ⇒ Object
Returns the null device (aka bitbucket) on your platform.
-
.tail(filename, num_lines = 10) ⇒ Object
In block form, yields the last
num_lines
of filefilename
. -
.touch(filename) ⇒ Object
Creates the 0 byte file
filename
. -
.wc(filename, option = 'all') ⇒ Object
With no arguments, returns a four element array consisting of the number of bytes, characters, words and lines in filename, respectively.
-
.whereis(program, path = ENV['PATH']) ⇒ Object
In block form, yields each
program
withinpath
. -
.which(program, path = ENV['PATH']) ⇒ Object
Looks for the first occurrence of
program
withinpath
.
Class Method Details
.binary?(file) ⇒ Boolean
Returns whether or not file
is a binary file. Note that this is not guaranteed to be 100% accurate. It performs a “best guess” based on a simple test of the first File.blksize
characters. – Based on code originally provided by Ryan Davis (which, in turn, is based on Perl’s -B switch).
46 47 48 49 |
# File 'lib/ptools.rb', line 46 def self.binary?(file) s = (File.read(file, File.stat(file).blksize) || "").split(//) ((s.size - s.grep(" ".."~").size) / s.size.to_f) > 0.30 end |
.head(filename, num_lines = 10) ⇒ Object
In block form, yields the first num_lines
from filename
. In non-block form, returns an Array of num_lines
130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/ptools.rb', line 130 def self.head(filename, num_lines=10) a = [] IO.foreach(filename){ |line| break if num_lines <= 0 num_lines -= 1 if block_given? yield line else a << line end } return a.empty? ? nil : a # Return nil in block form end |
.middle(filename, from = 10, to = 20) ⇒ Object
In block form, yields line from
up to line to
. In non-block form returns an Array of lines from from
to to
.
147 148 149 150 151 152 153 |
# File 'lib/ptools.rb', line 147 def self.middle(filename, from=10, to=20) if block_given? IO.readlines(filename)[from-1..to-1].each{ |line| yield line } else IO.readlines(filename)[from-1..to-1] end end |
.nl_convert(filename, newfilename = filename, platform = "dos") ⇒ Object
Converts a text file from one OS platform format to another, ala ‘dos2unix’. Valid values for ‘format’, which are case insensitve, include:
-
MS Windows -> dos, windows, win32, mswin
-
Unix/BSD -> unix, linux, bsd
-
Mac -> mac, macintosh, apple, osx
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, an ArgumentError is raised.
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/ptools.rb', line 184 def self.nl_convert(filename, newfilename=filename, platform="dos") unless File.ftype(filename) == "file" raise TypeError, "Only valid for plain text files" end if platform =~ /dos|windows|win32|mswin/i format = "\cM\cJ" elsif platform =~ /unix|linux|bsd/i format = "\cJ" elsif platform =~ /mac|apple|macintosh|osx/i format = "\cM" else raise ArgumentError, "Invalid platform string" end orig = $\ $\ = format if filename == newfilename require "ftools" require "tempfile" tf = Tempfile.new("temp") tf.open IO.foreach(filename){ |line| line.chomp! tf.print line } tf.close File.delete(filename) File.copy(tf.path,filename) else nf = File.new(newfilename,"w+") IO.foreach(filename){ |line| line.chomp! nf.print line } nf.close end $\ = orig self end |
.null ⇒ Object
Returns the null device (aka bitbucket) on your platform. On most Unix-like systems this is ‘/dev/null’, on Windows it’s ‘NUL’, etc. – Based on information from en.wikipedia.org/wiki//dev/null
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/ptools.rb', line 26 def self.null case RUBY_PLATFORM when /mswin/i 'NUL' when /amiga/i 'NIL:' when /openvms/i 'NL:' else '/dev/null' end end |
.tail(filename, num_lines = 10) ⇒ Object
In block form, yields the last num_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. Also note that ‘tail -f’ functionality is not present.
162 163 164 165 166 167 168 169 170 |
# File 'lib/ptools.rb', line 162 def self.tail(filename, num_lines=10) if block_given? IO.readlines(filename).reverse[0..num_lines-1].reverse.each{ |line| yield line } else IO.readlines(filename).reverse[0..num_lines-1].reverse end end |
.touch(filename) ⇒ Object
Creates the 0 byte file filename
.
229 230 231 232 |
# File 'lib/ptools.rb', line 229 def self.touch(filename) File.open(filename, 'w'){} self end |
.wc(filename, option = 'all') ⇒ Object
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’.
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/ptools.rb', line 240 def self.wc(filename, option='all') option.downcase! valid = %w/all bytes characters chars lines words/ unless valid.include?(option) raise ArgumentError, "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(program, path = ENV['PATH']) ⇒ Object
In block form, yields each program
within path
. In non-block form, returns an array of each program
within path
.
On Windows, it looks for executables ending with the suffixes defined in your PATHEXT environment variable, or ‘.exe’, ‘.bat’ and ‘.com’ if that isn’t defined, which you may optionally include in program
.
Returns nil if not found.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/ptools.rb', line 98 def self.whereis(program, path=ENV['PATH']) dirs = [] programs = program.to_a # If no file extension is provided on Windows, try the WIN32EXT's in turn if IS_WINDOWS && File.extname(program).empty? unless WIN32EXTS.include?(File.extname(program).downcase) WIN32EXTS.each{ |ext| programs.push(program + ext) } end end path.split(File::PATH_SEPARATOR).each{ |dir| programs.each{ |prog| file = File.join(dir,prog) file.tr!('/', File::ALT_SEPARATOR) if File::ALT_SEPARATOR if File.executable?(file) && !File.directory?(file) if block_given? yield file else dirs << file end end } } dirs.empty? ? nil : dirs.uniq end |
.which(program, path = ENV['PATH']) ⇒ Object
Looks for the first occurrence of program
within path
.
On Windows, it looks for executables ending with the suffixes defined in your PATHEXT environment variable, or ‘.exe’, ‘.bat’ and ‘.com’ if that isn’t defined, which you may optionally include in program
.
Returns nil if not found.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/ptools.rb', line 59 def self.which(program, path=ENV['PATH']) programs = program.to_a # If no file extension is provided on Windows, try the WIN32EXT's in turn if IS_WINDOWS && File.extname(program).empty? unless WIN32EXTS.include?(File.extname(program).downcase) WIN32EXTS.each{ |ext| programs.push(program + ext) } end end # Catch the first path found, or nil location = catch(:done){ path.split(File::PATH_SEPARATOR).each{ |dir| programs.each{ |prog| f = File.join(dir, prog) if File.executable?(f) && !File.directory?(f) location = File.join(dir, prog) location.tr!('/', File::ALT_SEPARATOR) if File::ALT_SEPARATOR throw(:done, location) end } } nil # Evaluate to nil if not found } location end |