Module: Corefines::String::RelativePathFrom

Defined in:
lib/corefines/string.rb

Instance Method Summary collapse

Instance Method Details

#relative_path_from(base_dir) ⇒ String

Returns a relative path from the given base_dir to the path represented by this str. This method doesn't access the filesystem and assumes no symlinks.

If str is absolute, then base_dir must be absolute too. If str is relative, then base_dir must be relative too.

Examples:

'/home/flynn/tron'.relative_path_from('/home')  # => flynn/tron
'/home'.relative_path_from('/home/flynn/tron')  # => ../..

Parameters:

  • base_dir (String, Pathname)

    the base directory to calculate relative path from.

Returns:

  • (String)

    a relative path from base_dir to this str.

Raises:

  • ArgumentError if it cannot find a relative path.


301
302
303
304
305
306
307
# File 'lib/corefines/string.rb', line 301

module RelativePathFrom
  refine ::String do
    def relative_path_from(base_dir)
      ::Pathname.new(self).relative_path_from(::Pathname.new(base_dir)).to_s
    end
  end
end