Class: Dir

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
mrbgems/mruby-dir/mrblib/dir.rb

Constant Summary

Constants included from Enumerable

Enumerable::NONE

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#all?, #any?, #chain, #chunk, #chunk_while, #collect, #count, #cycle, #detect, #drop, #drop_while, #each_cons, #each_entry, #each_slice, #each_with_index, #each_with_object, #entries, #filter_map, #find_all, #find_index, #first, #flat_map, #grep, #grep_v, #group_by, #hash, #include?, #inject, #lazy, #max, #max_by, #min, #min_by, #minmax, #minmax_by, #none?, #one?, #partition, #reject, #reverse_each, #sort, #sort_by, #sum, #take, #take_while, #tally, #to_h, #uniq, #zip

Class Method Details

.chdir(path, &block) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'mrbgems/mruby-dir/mrblib/dir.rb', line 67

def chdir(path, &block)
  if block
    wd = self.getwd
    begin
      self._chdir(path)
      block.call(path)
    ensure
      self._chdir(wd)
    end
  else
    self._chdir(path)
  end
end

.children(path) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'mrbgems/mruby-dir/mrblib/dir.rb', line 34

def children(path)
  a = []
  self.open(path) do |d|
    while s = d.read
      a << s unless s == "." || s == ".."
    end
  end
  a
end

.entries(path) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'mrbgems/mruby-dir/mrblib/dir.rb', line 24

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

.foreach(path, &block) ⇒ Object



44
45
46
47
48
49
# File 'mrbgems/mruby-dir/mrblib/dir.rb', line 44

def foreach(path, &block)
  return to_enum(:foreach, path) unless block
  self.open(path) do |d|
    d.each(&block)
  end
end

.open(path, &block) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'mrbgems/mruby-dir/mrblib/dir.rb', line 51

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

Instance Method Details

#each(&block) ⇒ Object



4
5
6
7
8
9
10
# File 'mrbgems/mruby-dir/mrblib/dir.rb', line 4

def each(&block)
  return to_enum(:each) unless block
  while s = self.read
    block.call(s)
  end
  self
end

#each_child(&block) ⇒ Object



12
13
14
15
16
17
18
# File 'mrbgems/mruby-dir/mrblib/dir.rb', line 12

def each_child(&block)
  return to_enum(:each_child) unless block
  while s = self.read
    block.call(s) unless s == "." || s == ".."
  end
  self
end