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

call-seq:

Dir.chdir(string) -> 0
Dir.chdir(string) { |path| block } -> obj

Changes the current working directory of the calling process to the given string. When called with a block, changes to the directory, executes the block, then changes back to the original directory. Returns the value of the block.

Dir.chdir("/var/spool/mail")
Dir.chdir("/tmp") { Dir.pwd }   #=> "/tmp"


105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'mrbgems/mruby-dir/mrblib/dir.rb', line 105

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

.foreach(path, &block) ⇒ Object

call-seq:

Dir.foreach(dirname) { |filename| block } -> nil
Dir.foreach(dirname)                      -> enumerator

Calls the block once for each entry in the named directory, passing the filename of each entry as a parameter to the block.

Dir.foreach("testdir") { |x| puts "Got #{x}" }


58
59
60
61
62
63
# File 'mrbgems/mruby-dir/mrblib/dir.rb', line 58

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

call-seq:

Dir.open(string) -> aDir
Dir.open(string) { |aDir| block } -> obj

With no block, open is a synonym for Dir.new. If a block is present, it is passed aDir as a parameter. The directory is closed at the end of the block, and Dir.open returns the value of the block.

Dir.open("testdir") { |d| d.each { |x| puts "Got #{x}" } }


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'mrbgems/mruby-dir/mrblib/dir.rb', line 76

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

call-seq:

dir.each { |filename| block } -> dir
dir.each                      -> enumerator

Calls the block once for each entry in this directory, passing the filename of each entry as a parameter to the block.

d = Dir.new("testdir")
d.each { |x| puts "Got #{x}" }


15
16
17
18
19
20
21
# File 'mrbgems/mruby-dir/mrblib/dir.rb', line 15

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

#each_child(&block) ⇒ Object

call-seq:

dir.each_child { |filename| block } -> dir
dir.each_child                      -> enumerator

Calls the block once for each entry in this directory except for "." and "..", passing the filename of each entry as a parameter to the block.

d = Dir.new("testdir")
d.each_child { |x| puts "Got #{x}" }


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

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