Class: Dir

Inherits:
Object show all
Defined in:
lib/backports/1.8.7/dir/foreach.rb,
lib/backports/2.5.0/dir/children.rb,
lib/backports/2.5.0/dir/each_child.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.children(*args) ⇒ Object



3
4
5
# File 'lib/backports/2.5.0/dir/children.rb', line 3

def self.children(*args)
  entries(*args) - Backports::EXCLUDED_CHILDREN
end

.each_child(*args) ⇒ Object



3
4
5
6
# File 'lib/backports/2.5.0/dir/each_child.rb', line 3

def self.each_child(*args)
  return to_enum(__method__, *args) unless block_given?
  foreach(*args) { |f| yield f unless Backports::EXCLUDED_CHILDREN.include? f }
end

.home(user = "") ⇒ Object



2
3
4
# File 'lib/backports/1.9.2/dir/home.rb', line 2

def Dir.home(user = "")
  File.expand_path "~#{user}"
end

.mktmpdir(prefix_suffix = nil, tmpdir = nil) ⇒ Object

Raises:

  • (NoMethodError)


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
# File 'lib/backports/1.8.7/stdlib/tmpdir.rb', line 3

def mktmpdir(prefix_suffix=nil, tmpdir=nil)
  raise NoMethodError, "undefined method `mktmpdir' for Dir:Class; you must require 'tmpdir'" unless respond_to? :tmpdir

  case prefix_suffix
  when nil
    prefix = "d"
    suffix = ""
  when String
    prefix = prefix_suffix
    suffix = ""
  when Array
    prefix = prefix_suffix[0]
    suffix = prefix_suffix[1]
  else
    raise ArgumentError, "unexpected prefix_suffix: #{prefix_suffix.inspect}"
  end
  tmpdir ||= Dir.tmpdir
  t = Time.now.strftime("%Y%m%d")
  n = nil
  begin
    path = "#{tmpdir}/#{prefix}#{t}-#{$$}-#{Kernel.rand(0x100000000).to_s(36)}"
    path << "-#{n}" if n
    path << suffix
    Dir.mkdir(path, 0700)
  rescue Errno::EEXIST
    n ||= 0
    n += 1
    retry
  end

  if block_given?
    begin
      yield path
    ensure
      FileUtils.remove_entry_secure path
    end
  else
    path
  end
end

Instance Method Details

#childrenObject



7
8
9
# File 'lib/backports/2.5.0/dir/children.rb', line 7

def children
  self.class.children(path)
end

#each_child(&block) ⇒ Object



8
9
10
11
12
13
# File 'lib/backports/2.5.0/dir/each_child.rb', line 8

def each_child(&block)
  return to_enum(__method__) unless block_given?

  Dir.each_child(path, &block)
  self
end