时间:2010-07-26 17:58:08

标签: ruby file

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:1)

您可以根据需要将其设置为复杂,但此基本脚本会将文件后缀与您可以使用文件路径调用的方法匹配。

# a conversion method can be used for each file type if you want to
# make the code more readable or if you need to rearrange filenames.
def htm_convert file
  "HTML #{file}"
end

# file suffix as key, lambda as value, the last uses an external method
routines = {
  :log => lambda {|file| puts "LOG #{file}"},
  :rb => lambda {|file| puts "RUBY #{file}"},
  :haml => lambda {|file| puts "HAML #{file}"},
  :htm => lambda {|file| puts htm_convert(file) }
}

# this loops recursively through the directory and sub folders
Dir['**/*.*'].each do |f|
  suffix = f.split(".")[-1]
  if routine = routines[suffix.to_sym]
    routine.call(f)
  else
    puts "UNPROCESSED -- #{f}"
  end
end