How do I convert an absolute path into a relative path

N

Nigel Wilkinson

Hi folks

I have an absolute path to a specific file but I want to convert it to a
path that's relative a specific directory. How can I do this?

Cheers
Nigel
 
S

Stefan Lang

Hi folks

I have an absolute path to a specific file but I want to convert it to a
path that's relative a specific directory. How can I do this?

Use the Pathname library (comes with Ruby):

irb(main):003:0> require 'pathname'
irb(main):004:0> p1 = Pathname.new("/home/me/foo/bar/baz")
=> #<Pathname:/home/me/foo/bar/baz>
irb(main):005:0> p2 = Pathname.new("/home/me")
=> #<Pathname:/home/me>
irb(main):006:0> p3 = p1.relative_path_from(p2)
=> #<Pathname:foo/bar/baz>

`ri Pathname' will show you all available methods.

HTH,
Stefan
 
N

Nigel Wilkinson

Use the Pathname library (comes with Ruby):

irb(main):003:0> require 'pathname'
irb(main):004:0> p1 = Pathname.new("/home/me/foo/bar/baz")
=> #<Pathname:/home/me/foo/bar/baz>
irb(main):005:0> p2 = Pathname.new("/home/me")
=> #<Pathname:/home/me>
irb(main):006:0> p3 = p1.relative_path_from(p2)
=> #<Pathname:foo/bar/baz>

`ri Pathname' will show you all available methods.

HTH,
Stefan


Thanks
Nigel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top