Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How do I convert an absolute path into a relative path

Reply
Thread Tools

How do I convert an absolute path into a relative path

 
 
Nigel Wilkinson
Guest
Posts: n/a
 
      07-24-2005
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


 
Reply With Quote
 
 
 
 
Stefan Lang
Guest
Posts: n/a
 
      07-24-2005
On Sunday 24 July 2005 11:59, Nigel Wilkinson wrote:
> 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


 
Reply With Quote
 
 
 
 
Nigel Wilkinson
Guest
Posts: n/a
 
      07-25-2005


--On Sunday, July 24, 2005 19:33:12 +0900 Stefan Lang <(E-Mail Removed)>
wrote:

> On Sunday 24 July 2005 11:59, Nigel Wilkinson wrote:
>> 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
>



Thanks
Nigel



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How does one get an absolute absolute file path? James Byrne Ruby 3 09-14-2010 06:02 PM
absolute path versus relative path in JSP Matt Java 3 07-08-2004 08:31 PM
Convert absolute positioning to relative? Vernon Compton HTML 10 03-02-2004 09:46 AM
Make a relative url path from an absolute path to another one Thomas Guettler Python 3 10-27-2003 04:41 PM



Advertisments