In Ruby we want to implement the method of traversing the specified directory, the method of the Internet is also very much, we can refer to reference, as shown in the following traverse.rb file content:
#!/usr/bin/ruby def traverse (filepath) if file.directory? (filepath) puts "Dirs:" + filepath Dir.foreach (filepath) do |filename| if filename! = "." and filename! = "..." Traverse (filepath + "/" + filename) end end else puts "Files:" + filepath end end #traverse ("d:/v Mware ") Traverse ("/usr/local/src ")
In fact, Ruby has defined the Find.find method to handle this situation, the above Ruby program can be implemented in its own way, the code appears more neat, as follows:
#!/usr/bin/ruby require ' find ' find.find ("/usr/local/src") do |filename| P Filenameend#traverse ("D:/vmware") Traverse ("/usr/local/src")
This article is from the "Fuqin Wine" blog, please make sure to keep this source http://yuhongchun.blog.51cto.com/1604432/1601278
Traversal of a specified directory file method in Ruby