From: Mike Wilson Date: 2001-02-04T22:41:08+09:00 Subject: [ruby-talk:10345] Re: Efficiency Check >From: "酵費七(Park, Hee-Sob)" >Reply-To: ruby-talk@netlab.co.jp >To: ruby-talk@netlab.co.jp (ruby-talk ML) >Subject: [ruby-talk:10339] Re: Efficiency Check >Date: Sun, 4 Feb 2001 16:10:07 +0900 > >Hi, > >"Jason" wrote in message >news:95iage$8n$1@nnrp1.deja.com... > > Hi All, > > > > Below is a directory wide (or file wide, depending on your preference) > > search and replace method, written in Ruby. Just so I know I'm on the > > right track, is that the quickest way to write the application (ie, is > > there a shortcut around creating an object named aFile, and then > > writing to the file? > > > > > > def sr(searchString, replaceString, fileName) > > if File.stat(fileName).file? > > aFile = File.open(fileName, "r").read.gsub(searchString, > > replaceString) > > File.open(fileName, "w").print(aFile) > > end > > end > > (ARGV.length - 2).times { |i| sr(ARGV[0], ARGV[1], ARGV[2+i])} > > > >How about this? > >#!/usr/local/bin/ruby -pi >BEGIN { > $searchString = ARGV.shift > $replaceString = ARGV.shift >} >gsub($searchString,$replaceString) > > > Jason > > > I'm curious as to why the BEGIN block is used here? Also, seeing as there's no error checking or anything, and you're assuming arguments, why not: #!/usr/local/bin/ruby -pi gsub(ARGV[0], ARGV[1]) For this, I'd probably just do $ /usr/local/bin/ruby -pi -e 'gsub(foo, bar)' somefile Mike Wilson Unix Administrator http://ruby.weblogs.com _________________________________________________________________ Get your FREE download of MSN Explorer at http://explorer.msn.com