Sha256: 8d3ffe7a710e389fdfbce583abb1d3b2e2a37581d284226f82e160f410b20606

Contents?: true

Size: 934 Bytes

Versions: 5

Compression:

Stored size: 934 Bytes

Contents

class Dir

  # TODO: Make instance method versions ?

  # Ascend a directory path.
  #
  #   Dir.ascend("/var/log") do |path|
  #     p path
  #   end
  #
  # _produces_
  #
  #   /var/log
  #   /var
  #   /
  #
  # CREDIT: Daniel Berger, Jeffrey Schwab

  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.
  #
  #   Dir.descend("/var/log") do |path|
  #     p path
  #   end
  #
  # _produces_
  #
  #   /
  #   /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

5 entries across 5 versions & 1 rubygems

Version Path
facets-2.7.0 lib/core/facets/dir/ascend.rb
facets-2.6.0 lib/core/facets/dir/ascend.rb
facets-2.5.0 lib/core/facets/dir/ascend.rb
facets-2.5.1 lib/core/facets/dir/ascend.rb
facets-2.5.2 lib/core/facets/dir/ascend.rb