Skip to content

Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method · added in Ruby 3.3

Dir#chdir

chdir → 0
chdir { ... } → object

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

  1. 3.3
See also .chdir .unlink #children .tmpdir also in 4.1 · 4.0 · 3.4