@{ Home >> Teknik >> Ruby << }

→[ Indhold | Links | Nyheder | Oracle | Programmering | Referencer | Teknik ]
CeDeT [se'de']
Shine on you crazy diamond!?
Ruby on Rails
A Comparison
Ruby vs. Perl vs. GnuWin32
  After having downloaded the files that make up the first (online) version of the book Programming Ruby - I wanted to make it browsable offline... As part of this excercise I needed to modify all the html-files, so that href="/pickaxe" was substituted with href="../index.html". I have Perl installed on my Windows XP machine, and to do this using Perl is easy (from the command line):
perl -i.bak -p -e "s/href=\"\/pickaxe\"/href=\"..\/index.html\"/g" *.html
Do an inline replace with saving a (.)backup of the file, do a print of each input line after executing the "s/ubstitution command" (There's a lot of character(\)-escaping going on to make the " and / understood properly inside the command to be executed). Do it on all *.html files (perl performs the file name wildcard expansion and matching for you).

Almost the same thing (creating .new files instead of .bak files) can be performed using DOS commands and the aid of sed (from GnuWin32):
for %f in (*.html) do type %f |
sed "s/href=\"\/pickaxe\"/href=\"..\/index.html\"/g" >%f.new
The ruby program I "cooked up" for doing the same thing as the perl one-liner:
require 'ftools' # required for move

d=Dir.open('.') { |f| f.grep /.html$/ }
# all *.html files in current directory
d.each do |f|
  File.move(f.to_s,f.to_s+".bak") # move to backup copy
  File.open(f.to_s+".bak","r") do |iFile| # read the back(ed) up file,
    iFile.each_line do |line|             # line by line
© 2009,2011 CeDeT - Sidst opdateret: 22. oktober 2011.
Valid XHTML 1.0!