Skip to content

Instantly share code, notes, and snippets.

@numbata
Created June 13, 2019 07:41
Show Gist options
  • Save numbata/0de2cdd5ddba4484041fe7401fd7b1ea to your computer and use it in GitHub Desktop.
Save numbata/0de2cdd5ddba4484041fe7401fd7b1ea to your computer and use it in GitHub Desktop.
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