Sha256: cbc58fc49eddd7254d533728a1ceb18b3fb9906301f109ed54b1f72b76e6e669

Contents?: true

Size: 1.59 KB

Versions: 6

Compression:

Stored size: 1.59 KB

Contents

#--
# Credit due to George Moschovitis <gm@navel.gr>
#++
class Dir

  # Recursively scan a directory and pass each file
  # to the given block.
  def self.ls_r(path='.', &block)
    list = []
    stoplist = ['.', '..']
    Dir.foreach(path) do |f|
      next if stoplist.include?(f)
      filename = path + '/' + f
      list << filename
      block.call(filename) if block
      if FileTest.directory?(filename) and not FileTest.symlink?(filename)
        list.concat( Dir.recurse(filename, &block) )
      end
    end
    list
  end

  class << self
    # Alias for Dir#ls_r
    alias_method( :recurse, :ls_r )
  end

end



#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#
=begin test

  require 'test/unit'

  # FIND TEST DIRECTORY
  paths = File.expand_path(File.dirname(__FILE__)).split('/')
  paths.size.downto(1) do |i|
    f = (paths.slice(0..i)+['test']).join('/')
    $TESTDIR = File.join(f,'FIXTURE') if File.directory?(f)
  end
  raise unless $TESTDIR

  class TCDir < Test::Unit::TestCase

    def test_ls_r
      td = Dir.pwd
      Dir.chdir $TESTDIR
        r = ["A", "A/B","A/B/C.txt", "A/B.txt", "A.txt"].collect{ |e| 
          File.join( 'ls_r', e )
        }
        fs = Dir.ls_r( 'ls_r' )
        assert_equal( r, fs, Dir.pwd  )
      Dir.chdir td
    end

    def test_recurse
      td = Dir.pwd
      Dir.chdir $TESTDIR
        r = ["A", "A/B","A/B/C.txt", "A/B.txt", "A.txt"].collect{ |e| 
          File.join( 'ls_r', e )
        }
        fs = Dir.recurse( 'ls_r' )
        assert_equal( r, fs, Dir.pwd )
      Dir.chdir td
    end

  end

=end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
facets-0.9.0 lib/nano/dir/self/ls_r.rb
facets-1.0.0 lib/facet/dir/self/ls_r.rb
facets-1.0.3 packages/core/lib/facet/dir/self/ls_r.rb
facets-1.2.0 lib/facets/core/dir/self/ls_r.rb
facets-1.1.0 lib/facet/dir/self/ls_r.rb
facets-1.2.1 lib/facets/core/dir/self/ls_r.rb