Rubyの復習その1

#!/usr/bin/ruby
require "find"      #Findモジュールのファイル名の指定して取り込む

from = "ABC"       # local variable ローカル変数の指定
to = "XYZ" 
Find.find(".") do | f |
 if File.file?(f) && f =~ /\.dat$/i
  dirname = f.sub(/(.*\/)(.*)/,\1')  # dont use "\1" "を使うとダメ
  print "dir is ",dirname,"\n"

  filename = f.sub(/(.*\/)(.*)/,'\2')
  print "dir is ",filename,"\n"



  fp = open(f,"r+")
  fpout = open(f + ".bk","w+")

  all = fp.readlines
  all.each do | i | 
   if i =~ /#{from}/   # ShikiTenkai #{}は式展開
    i.gsub!(from,to)
   end 
  end
  fpout.write(all)
  fp.close
  fpout.close
 end
end