summaryrefslogtreecommitdiffstats
path: root/web/server/h2o/libh2o/deps/mruby-dir/mrblib/dir.rb
blob: 065ca1c22a55be195a733bb71ad8471f0c449b27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class Dir
  def each(&block)
    while s = self.read
      block.call(s)
    end
    self
  end

  alias pos tell
  alias pos= seek

  def self.entries(path)
    a = []
      self.open(path) { |d|
      while s = d.read
        a << s
      end
    }
    a
  end

  def self.foreach(path, &block)
    if block
      self.open(path).each { |f| block.call(f) }
    else
      self.open(path).each
    end
  end

  def self.open(path, &block)
    if block
      d = self.new(path)
      begin
        block.call(d)
      ensure
        d.close
      end
    else
      self.new(path)
    end
  end

  def self.chdir(path, &block)
    my = self # workaround for https://github.com/mruby/mruby/issues/1579
    if block
      wd = self.getwd
      begin
        self._chdir(path)
        block.call(path)
      ensure
        my._chdir(wd)
      end
    else
      self._chdir(path)
    end
  end

  class << self
    alias exists? exist?
    alias pwd getwd
    alias rmdir delete
    alias unlink delete
  end
end