class Dir

supplement/dir.rb – More Dir methods

Public Class Methods

current() → dir click to toggle source

Current directory as a Dir object.

VALUE
rb_dir_s_current( VALUE dir)
{
    return rb_funcall( dir, rb_intern( "new"), 1, rb_str_new( ".", 1));
}
mkdir!( path, modes = nil) → str or nil click to toggle source

Make a directory and all subdirectories if needed.

If you specifiy modes, be sure that you have the permission to create subdirectories.

Returns the path demanded if the directory was created and nil if it existed before.

VALUE
rb_dir_s_mkdir_bang( int argc, VALUE *argv)
{
    VALUE path, modes;

    rb_scan_args( argc, argv, "11", &path, &modes);
    if (!rb_file_directory_p( rb_cFile, path)) {
        VALUE parent[2];

        parent[ 0] = rb_file_dirname( path);
        parent[ 1] = modes;
        rb_dir_s_mkdir_bang( 2, parent);
        if (!id_mkdir)
            id_mkdir = rb_intern( "mkdir");
        if (NIL_P(modes))
            rb_funcall( rb_cDir, id_mkdir, 1, path);
        else
            rb_funcall( rb_cDir, id_mkdir, 2, path, modes);
        return path;
    }
    return Qnil;
}

Public Instance Methods

entries!() → dir click to toggle source

Entries without "." and "..".

VALUE
rb_dir_entries_bang( VALUE self)
{
    VALUE e;

    e = rb_funcall( self, rb_intern( "entries"), 0);
    rb_ary_delete( e, rb_str_new( ".", 1));
    rb_ary_delete( e, rb_str_new( "..", 2));
    return e;
}
nuke!( name) → nil click to toggle source

Delete a directory, all its contents and all subdirectories. WARNING! This can cause serious damage.

# File lib/supplement/dir.rb, line 15
def nuke! n
  (new n).entries!.each { |e|
    p = File.join n, e
    if File.directory? p then
      nuke! p
    else
      File.unlink p
    end
  }
  rmdir n
  nil
end
rmpath( name) → nil click to toggle source

Delete as many empty directories as possible.

# File lib/supplement/dir.rb, line 33
def rmpath n
  case n
    when "/", "." then
    else
      if (new n).entries!.empty? then
        rmdir n
        d = File.dirname n
        rmpath d
      end
  end
  nil
end