Ruby遍历文件夹

来源:互联网 发布:淘宝美工课程介绍 编辑:程序博客网 时间:2024/06/10 08:29

原先需要自己手写一个递归算法:

def traverse_dir(file_path)    if File.directory? file_path        Dir.foreach(file_path) do |file|            if file !="." and file !=".."                traverse_dir(file_path+"/"+file)            end        end    else        puts "File:#{File.basename(file_path)}, Size:#{File.size(file_path)}"    endend

而今Ruby已经提供了一个Find.find方法,封装的更好,可以直接按照下面的例子使用:

require 'find'Find.find('/Users/happy') do |path|  puts pathend


0 0
原创粉丝点击