vendor/mruby-source/mrbgems/mruby-dir/mrblib/dir.rb
raw| 1 | class Dir |
| 2 | include Enumerable |
| 3 | |
| 4 | def each(&block) |
| 5 | while s = self.read |
| 6 | block.call(s) |
| 7 | end |
| 8 | self |
| 9 | end |
| 10 | |
| 11 | alias each_child each |
| 12 | alias pos tell |
| 13 | alias pos= seek |
| 14 | |
| 15 | class << self |
| 16 | def entries(path) |
| 17 | a = [] |
| 18 | self.open(path) do |d| |
| 19 | while s = d.read |
| 20 | a << s |
| 21 | end |
| 22 | end |
| 23 | a |
| 24 | end |
| 25 | alias children entries |
| 26 | |
| 27 | def foreach(path, &block) |
| 28 | self.open(path) do |d| |
| 29 | d.each(&block) |
| 30 | end |
| 31 | end |
| 32 | |
| 33 | def open(path, &block) |
| 34 | if block |
| 35 | d = self.new(path) |
| 36 | begin |
| 37 | block.call(d) |
| 38 | ensure |
| 39 | begin |
| 40 | d.close |
| 41 | rescue IOError |
| 42 | end |
| 43 | end |
| 44 | else |
| 45 | self.new(path) |
| 46 | end |
| 47 | end |
| 48 | |
| 49 | def chdir(path, &block) |
| 50 | if block |
| 51 | wd = self.getwd |
| 52 | begin |
| 53 | self._chdir(path) |
| 54 | block.call(path) |
| 55 | ensure |
| 56 | self._chdir(wd) |
| 57 | end |
| 58 | else |
| 59 | self._chdir(path) |
| 60 | end |
| 61 | end |
| 62 | |
| 63 | alias exists? exist? |
| 64 | alias pwd getwd |
| 65 | alias rmdir delete |
| 66 | alias unlink delete |
| 67 | end |
| 68 | end |