Parent

Dir

Public Class Methods

ascend(dir, inclusive=true, &blk) click to toggle source

Ascend a directory path.

  Dir.ascend("/var/log") do |path|
    p path
  end

produces

  /var/log
  /var
  /

CREDIT: Daniel Berger, Jeffrey Schwab

TODO: make it work with windows too use FileTest.root?

    # File lib/core/facets/dir/ascend.rb, line 22
22:   def self.ascend(dir, inclusive=true, &blk)
23:     dir = dir.dup
24:     blk.call(dir) if inclusive
25:     ri = dir.rindex('/')
26:     while ri
27:       dir = dir.slice(0...ri)
28:       if dir == ""
29:         blk.call('/') ; break
30:       end
31:       blk.call( dir )
32:       ri = dir.rindex('/')
33:     end
34:   end
descend(path) click to toggle source

Descend a directory path.

  Dir.descend("/var/log") do |path|
    p path
  end

produces

  /
  /var
  /var/log

CREDIT: Daniel Berger, Jeffrey Schwab

    # File lib/core/facets/dir/ascend.rb, line 50
50:   def self.descend(path) #:yield:
51:     paths = path.split('/')
52:     paths.size.times do |n|
53:       pth = File.join(*paths[0..n])
54:       pth = "/" if pth == ""
55:       yield(pth)
56:     end
57:   end
ls_r(path='.', &block) click to toggle source

Same as Dir#recurse.

    # File lib/core/facets/dir/recurse.rb, line 27
27:   def self.ls_r(path='.', &block)
28:     recurse(path, &block)
29:   end
multiglob(*patterns) click to toggle source

Like glob but can take multiple patterns.

  Dir.multiglob( '*.rb', '*.py' )

Rather then constants for options multiglob accepts a trailing options hash of symbol keys.

  :noescape    File::FNM_NOESCAPE
  :casefold    File::FNM_CASEFOLD
  :pathname    File::FNM_PATHNAME
  :dotmatch    File::FNM_DOTMATCH
  :strict      File::FNM_PATHNAME && File::FNM_DOTMATCH

It also has an option for recurse.

  :recurse     Recurively include contents of directories.

For example

  Dir.multiglob( '*', :recurse => true )

would have the same result as

  Dir.multiglob('**/*')
    # File lib/core/facets/dir/multiglob.rb, line 40
40:   def self.multiglob(*patterns)
41:     options  = (Hash === patterns.last ? patterns.pop : {})
42: 
43:     if options.delete(:recurse)
44:       #patterns += patterns.collect{ |f| File.join(f, '**', '**') }
45:       multiglob_r(*patterns)
46:     end
47: 
48:     bitflags = 0
49:     bitflags |= File::FNM_NOESCAPE if options[:noescape]
50:     bitflags |= File::FNM_CASEFOLD if options[:casefold]
51:     bitflags |= File::FNM_PATHNAME if options[:pathname] or options[:strict]
52:     bitflags |= File::FNM_DOTMATCH if options[:dotmatch] or options[:strict]
53: 
54:     patterns = [patterns].flatten.compact
55: 
56:     if options[:recurse]
57:       patterns += patterns.collect{ |f| File.join(f, '**', '**') }
58:     end
59: 
60:     files = []
61:     files += patterns.collect{ |pattern| Dir.glob(pattern, bitflags) }.flatten.uniq
62: 
63:     return files
64:   end
multiglob_r(*patterns) click to toggle source

The same as multiglob, but recusively includes directories.

  Dir.multiglob_r( 'folder' )

is equivalent to

  Dir.multiglob( 'folder', :recurse=>true )

The effect of which is

  Dir.multiglob( 'folder', 'folder/**/**' )
    # File lib/core/facets/dir/multiglob.rb, line 78
78:   def self.multiglob_r(*patterns)
79:     options = (Hash === patterns.last ? patterns.pop : {})
80:     matches = multiglob(*patterns)
81:     directories = matches.select{ |m| File.directory?(m) }
82:     matches += directories.collect{ |d| multiglob_r(File.join(d, '**'), options) }.flatten
83:     matches.uniq
84:     #options = (Hash === patterns.last ? patterns.pop : {})
85:     #options[:recurse] = true
86:     #patterns << options
87:     #multiglob(*patterns)
88:   end
parent?(parent_path, child_path) click to toggle source

Is a path parental to another?

TODO: Needs improvement. TODO: Instance version?

    # File lib/core/facets/dir/parent.rb, line 8
 8:   def self.parent?(parent_path, child_path)
 9:     %^#{Regexp.escape(parent_path)}| =~ child_path
10:   end
recurse(path='.', &block) click to toggle source

Recursively scan a directory and pass each file to the given block.

CREDIT: George Moschovitis

TODO: If fully compatible, reimplement as alias of Find.find, or just copy and paste Find.find code here if it looks more robust.

    # File lib/core/facets/dir/recurse.rb, line 11
11:   def self.recurse(path='.', &block)
12:     list = []
13:     stoplist = ['.', '..']
14:     Dir.foreach(path) do |f|
15:       next if stoplist.include?(f)
16:       filename = (path == '.' ? f : path + '/' + f)
17:       list << filename
18:       block.call(filename) if block
19:       if FileTest.directory?(filename) and not FileTest.symlink?(filename)
20:         list.concat( Dir.recurse(filename, &block) )
21:       end
22:     end
23:     list
24:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.