So I was asking myself the question… What ruby run time options
would I add to make oneliners easier.
Basically awk started it all with the notion of a loop that iterated
over a file allowing patterns and actions per line…
This is enshrined in ruby as…
ruby -nple ‘do whatever with $’
or…
ruby -nplae 'do whatever with $ split into $F’
However, the ruby variants…
IO.read( filename).gsub( %r{whatever}mx){|match| }
and
IO.read( filename).scan( %r{whatever}mx){|match| }
are way more powerful.
The next Big Thing in one liners was the perl -i.bak option. Ruby has it
too.
Invariably the -i.bak option is used in conjunction with the “find” and
“xargs”
commands to find the files.
Like so…
find . -name ‘*.[hc]’ | xargs ruby -i.bak -nple ‘whatever’
But Ruby has an excellent and way less cryptic find facility…
require ‘find’
Find.find( startPath) do |path|
next unless path =~ %r{ .[hc] $ }x
Do something with path
end
Wouldn’t it be nice if we could …
ruby --find startPath --matching pathRegex --scan scanRegex -e action
or
ruby --find startPath --matching pathRegex -i.bak --gsub searchRegex -e
“replaceString”
with suitable one letter ‘-’ options replacing the --long options for
golfing.
ruby --find startPath --matching pathRegex --prune ‘pruneRegex’ --scan
scanRegex -e action
Would effectively expand to something vaguely like…
require ‘find’
Find.find( *startPath.split(’,’)) do |path|
next unless path.match( pathRegex)
Find.prune if path.match( pruneRegex)
IO.read(path).scan(scanRegex){|match| action}
end
and…
ruby --find startPath --matching pathRegex -i.bak --gsub searchRegex -e
“replaceString”
Would effectively expand to something vaguely like…
require ‘find’
Find.find( *startPath.split(’,’)) do |path|
next unless path.match( pathRegex)
Find.prune if path.match( pruneRegex)
backup = path + ‘.bak’
File.rename( path, backup)
open( path, ‘w’) do |outf|
outf.syswrite( IO.read( backup).gsub(searchRegex){|match|
replaceString})
end
end
Any suggestions before I write up an RCR?
John C. Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : [email protected]
New Zealand