Sha256: 1652303b6a11abece50a48a1e1cc0a45ee1d822358b1a839d930387bd07284d3

Contents?: true

Size: 1014 Bytes

Versions: 7

Compression:

Stored size: 1014 Bytes

Contents

class Dir

  # TODO: Make instance method versions ?

  # Ascend a directory path.
  #
  #   a = []
  #
  #   Dir.ascend("/var/log") do |path|
  #     a << path
  #   end
  #
  #   a  #=> ['/var/log', '/var', '/']
  #
  # CREDIT: Daniel Berger, Jeffrey Schwab
  #
  # TODO: make it work with windows too
  # use FileTest.root?

  def self.ascend(dir, inclusive=true, &blk)
    dir = dir.dup
    blk.call(dir) if inclusive
    ri = dir.rindex('/')
    while ri
      dir = dir.slice(0...ri)
      if dir == ""
        blk.call('/') ; break
      end
      blk.call( dir )
      ri = dir.rindex('/')
    end
  end

  # Descend a directory path.
  #
  #   d = []
  #
  #   Dir.descend("/var/log") do |path|
  #     d << path
  #   end
  #
  #   d  #=> ['/', '/var', '/var/log']
  #
  # CREDIT: Daniel Berger, Jeffrey Schwab

  def self.descend(path) #:yield:
    paths = path.split('/')
    paths.size.times do |n|
      pth = File.join(*paths[0..n])
      pth = "/" if pth == ""
      yield(pth)
    end
  end

end

Version data entries

7 entries across 6 versions & 1 rubygems

Version Path
facets-2.9.3 lib/core/facets/dir/ascend.rb
facets-2.9.2 lib/core/facets/dir/ascend.rb
facets-2.9.2 src/core/facets/dir/ascend.rb
facets-2.9.1 lib/core/facets/dir/ascend.rb
facets-2.9.0 lib/core/facets/dir/ascend.rb
facets-2.9.0.pre.2 lib/core/facets/dir/ascend.rb
facets-2.9.0.pre.1 lib/core/facets/dir/ascend.rb