Created
June 13, 2019 07:41
-
-
Save numbata/0de2cdd5ddba4484041fe7401fd7b1ea to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'pathname' | |
| def rwalk(path) | |
| current_directory = Pathname.new(path) | |
| breadcrumbs = [[current_directory, Dir.foreach(current_directory)]] | |
| Enumerator.new do |y| | |
| loop do | |
| current_directory, current_iterator = breadcrumbs.last | |
| begin | |
| fname = current_iterator.next | |
| rescue StopIteration | |
| breadcrumbs.pop | |
| break if breadcrumbs.empty? | |
| next | |
| end | |
| next if fname == '.' || fname == '..' | |
| fullpath = current_directory.join(fname) | |
| if File.file?(fullpath) | |
| y << fullpath | |
| else | |
| breadcrumbs << [fullpath, Dir.foreach(fullpath)] | |
| end | |
| next | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment