Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
Pathname#expand_path
(?::String dir) -> ::Pathname
expand_path(dirpath = '.') → new_pathname
pathname_builtin.rb
View on GitHub ↗
# File tmp/rubies/ruby-4.1.0-preview1/pathname_builtin.rb, line 2182
def expand_path(...) self.class.new(File.expand_path(@path, ...)) end
Returns a new pathname containing the absolute path for self.
Evaluates a relative path with respect to the directory given by dirpath:
Example
Dir.chdir('/snap') # Default dirpath. Pathname('README').expand_path # => #<Pathname:/snap/README> Pathname('bin').expand_path # => #<Pathname:/snap/bin> Pathname('bin/../var').expand_path # => #<Pathname:/snap/var> # Cleaned. # Other dirpath. Pathname('../zip').expand_path('/usr/bin/ruby') # => #<Pathname:/usr/bin/zip> Dir.chdir('/usr/bin') Pathname('../../snap').expand_path(__FILE__) # => #<Pathname:/usr/snap>
Evaluates an absolute path without respect to dirpath:
Example
Pathname('/snap').expand_path # => #<Pathname:/snap> Pathname('/snap').expand_path.expand_path('nosuch') # => #<Pathname:/snap> Pathname('/snap/../snap').expand_path # => #<Pathname:/snap> # Cleaned.
More examples:
Example
Dir.chdir('/usr/bin') Pathname('../../snap').expand_path(__FILE__) # => #<Pathname:/usr/snap> Pathname('../../snap').expand_path # => #<Pathname:/snap>