Method: ShellHelpers::PathnameExt::Base#rel_path_to

Defined in:
lib/shell_helpers/pathname.rb

#rel_path_to(target = self.class.pwd, base: self.class.pwd, mode: :rel, clean_mode: :abs_clean, inside: false, **opts) ⇒ Object

path from self to target (warning: we always assume that we are one level up self, except if inside is true) bar=SH::Pathname.new("foo/bar"); baz=SH::Pathname.new("foo/baz") bar.rel_path_to(baz) #ShellHelpers::Pathname:baz bar.rel_path_to(baz, inside: true) #ShellHelpers::Pathname:../baz note: there is no real sense to use mode: :rel here, but we don't prevent it



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

def rel_path_to(target=self.class.pwd, base: self.class.pwd, mode: :rel, clean_mode: :abs_clean, inside: false, **opts)
  target=self.class.new(target) unless target.is_a?(self.class)
  sbase=opts[:source_base]||base
  smode=opts[:source_mode]||clean_mode
  tbase=opts[:target_base]||base
  tmode=opts[:target_mode]||clean_mode
  source=self.convert_path(base: sbase, mode: smode)
  target=target.convert_path(base: tbase, mode: tmode)
  from=inside ? source : source.dirname
  target.convert_path(base: from, mode: mode)
end