Method: Pathname#chdir

Defined in:
lib/pleasant_path/pathname.rb

#chdirself #chdir {|working_dir| ... } ⇒ Object

Changes the current working directory to the Pathname. If no block is given, this method returns the Pathname. Otherwise, the block is called with the Pathname, the original working directory is restored after the block exits, and this method returns the return value of the block.

Examples:

FileUtils.mkdir("dir1")
FileUtils.mkdir("dir2")

Pathname.new("dir1").chdir  # == Pathname.new("dir1")
Pathname.pwd                # == Pathname.new("dir1")

Pathname.new("dir2").chdir{|path| "in #{path}" }  # == "in dir2"
Pathname.pwd                                      # == Pathname.new("dir1")

Overloads:

  • #chdirself

    Returns:

    • (self)
  • #chdir {|working_dir| ... } ⇒ Object

    Yield Parameters:

    Yield Returns:

    Returns:

Raises:

  • (SystemCallError)

    if the Pathname does not point to an existing directory

See Also:



263
264
265
266
267
268
269
270
271
272
# File 'lib/pleasant_path/pathname.rb', line 263

def chdir
  if block_given?
    Dir.chdir(self) do |dir|
      yield dir.to_pathname
    end
  else
    Dir.chdir(self)
    self
  end
end