Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   How do I convert an absolute path into a relative path (http://www.velocityreviews.com/forums/t823155-how-do-i-convert-an-absolute-path-into-a-relative-path.html)

Nigel Wilkinson 07-24-2005 09:59 AM

How do I convert an absolute path into a relative path
 
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



Stefan Lang 07-24-2005 10:33 AM

Re: How do I convert an absolute path into a relative path
 
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



Nigel Wilkinson 07-25-2005 07:37 PM

Re: How do I convert an absolute path into a relative path
 


--On Sunday, July 24, 2005 19:33:12 +0900 Stefan Lang <langstefan@gmx.at>
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





All times are GMT. The time now is 02:07 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.