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.

Revisions

  1. numbata created this gist Jun 13, 2019.
    33 changes: 33 additions & 0 deletions directory_rwalk.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    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