Sha256: b4a3979482b0fedb5be31675e7dce70f81d91c361de548891ad26adee501bcf9

Contents?: true

Size: 965 Bytes

Versions: 7

Compression:

Stored size: 965 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
  #   CREDIT: 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
  #   CREDIT: 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 7 versions & 2 rubygems

Version Path
facets-2.4.0 lib/facets/dir/ascend.rb
facets-2.4.1 lib/facets/dir/ascend.rb
facets-2.4.2 lib/core/facets/dir/ascend.rb
facets-2.4.3 lib/core/facets/dir/ascend.rb
facets-2.4.4 lib/core/facets/dir/ascend.rb
facets-2.4.5 lib/core/facets/dir/ascend.rb
mack-facets-0.8.2 lib/gems/facets-2.4.5/lib/core/facets/dir/ascend.rb