How do I convert an absolute path into a relative path

Discussion in 'Ruby' started by Nigel Wilkinson, Jul 24, 2005.

  1. 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
    Nigel Wilkinson, Jul 24, 2005
    #1
    1. Advertising

  2. Nigel Wilkinson

    Stefan Lang Guest

    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
    Stefan Lang, Jul 24, 2005
    #2
    1. Advertising

  3. --On Sunday, July 24, 2005 19:33:12 +0900 Stefan Lang <>
    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
    Nigel Wilkinson, Jul 25, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. matthias s.

    Absolute or Relative path?

    matthias s., Jul 7, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    478
    matthias s.
    Jul 7, 2005
  2. Matt
    Replies:
    3
    Views:
    19,545
    Sudsy
    Jul 8, 2004
  3. Vernon Compton
    Replies:
    10
    Views:
    4,745
    Adrian Wood
    Mar 2, 2004
  4. Thomas Guettler
    Replies:
    3
    Views:
    754
    Andrei
    Oct 27, 2003
  5. James Byrne
    Replies:
    3
    Views:
    556
    James Byrne
    Sep 14, 2010
Loading...

Share This Page