source:
EcnlProtoTool/trunk/mrbgems/mruby-dir/mrblib/dir.rb
Last change on this file was 321, checked in by , 7 years ago | |
---|---|
|
|
File size: 1010 bytes |
Rev | Line | |
---|---|---|
[279] | 1 | class Dir |
2 | def each(&block) | |
3 | while s = self.read | |
4 | block.call(s) | |
5 | end | |
6 | self | |
7 | end | |
8 | ||
9 | alias pos tell | |
10 | alias pos= seek | |
11 | ||
12 | def self.entries(path) | |
13 | a = [] | |
14 | self.open(path) { |d| | |
15 | while s = d.read | |
16 | a << s | |
17 | end | |
18 | } | |
19 | a | |
20 | end | |
21 | ||
22 | def self.foreach(path, &block) | |
23 | if block | |
24 | self.open(path).each { |f| block.call(f) } | |
25 | else | |
26 | self.open(path).each | |
27 | end | |
28 | end | |
29 | ||
30 | def self.open(path, &block) | |
31 | if block | |
32 | d = self.new(path) | |
33 | begin | |
34 | block.call(d) | |
35 | ensure | |
36 | d.close | |
37 | end | |
38 | else | |
39 | self.new(path) | |
40 | end | |
41 | end | |
42 | ||
43 | def self.chdir(path, &block) | |
44 | my = self # workaround for https://github.com/mruby/mruby/issues/1579 | |
45 | if block | |
46 | wd = self.getwd | |
47 | begin | |
48 | self._chdir(path) | |
49 | block.call(path) | |
50 | ensure | |
51 | my._chdir(wd) | |
52 | end | |
53 | else | |
54 | self._chdir(path) | |
55 | end | |
56 | end | |
57 | ||
58 | class << self | |
59 | alias exists? exist? | |
60 | alias pwd getwd | |
61 | alias rmdir delete | |
62 | alias unlink delete | |
63 | end | |
64 | end |
Note:
See TracBrowser
for help on using the repository browser.