instance method · added in Ruby 3.3
Dir#chdir
() -> ::Integer
[T] () { () -> T } -> T
chdir → 0
chdir { ... } → object
dir.c
View on GitHub ↗
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
}
Changes the current working directory to self:
Example
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.
Ruby version changes
-
3.3
-
Dir#chdiradded for changing the directory to the directory specified by the providedDirobject. [Feature #19347]
-