Method: Dir#chdir

Defined in:
dir.c

#chdir0 #chdir { ... } ⇒ Object

Changes the current working directory to self:

Dir.pwd # => "/"
dir = Dir.new('example')
dir.chdir
Dir.pwd # => "/example"

With a block, temporarily changes the working directory:

  • Calls the block.

  • Changes to the given directory.

  • Executes the block (yields no args).

  • Restores the previous working directory.

  • Returns the block’s return value.

Uses Dir.fchdir if available, and Dir.chdir if not, see those methods for caveats.

Overloads:

  • #chdir0

    Returns:

    • (0)
  • #chdir { ... } ⇒ Object

    Yields:

    Returns:



1465
1466
1467
1468
1469
1470
1471
1472
1473
# File 'dir.c', line 1465

static VALUE
dir_chdir(VALUE dir)
{
#if defined(HAVE_FCHDIR) && defined(HAVE_DIRFD) && HAVE_FCHDIR && HAVE_DIRFD
    return dir_s_fchdir(rb_cDir, dir_fileno(dir));
#else
    return chdir_path(dir_get(dir)->path, false);
#endif
}