Google Groups Home
Help | Sign in
head/tail split for path
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  13 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Trans  
View profile
 More options Jul 26, 3:54 pm
Newsgroups: comp.lang.ruby
From: Trans <transf...@gmail.com>
Date: Sat, 26 Jul 2008 14:54:43 -0500
Local: Sat, Jul 26 2008 3:54 pm
Subject: head/tail split for path
I need to split a path by head/*tail.

Ex.

    File.head_tail_split('home/foo/bar')  #=> [ 'home', 'foo/bar' ]

Sure, I can write a clumsy loop like the following:

  def File.head_tail_split(fname)
    s = fname
    t = []
    h = nil
    until s == '.'
      t << h
      s, h = *split(s)
    end
    return h, File.join(*t.compact)
  end

But I'm betting there's a better way. Or maybe there's already an easy
way I'm overlooking?

T.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
David A. Black  
View profile
 More options Jul 26, 5:04 pm
Newsgroups: comp.lang.ruby
From: "David A. Black" <dbl...@rubypal.com>
Date: Sat, 26 Jul 2008 16:04:29 -0500
Local: Sat, Jul 26 2008 5:04 pm
Subject: Re: head/tail split for path
Hi --

Check out the Pathname feature:

   require 'pathname'

   p = Pathname.new("a/b/c")

You get p.basename and p.dirname. Both return Pathname objects, but
they're quite string-like and easily converted.

David

--
Rails training from David A. Black and Ruby Power and Light:
     Intro to Ruby on Rails  July 21-24      Edison, NJ
  *  Advancing With Rails    August 18-21    Edison, NJ
  * Co-taught by D.A. Black and Erik Kastner
See http://www.rubypal.com for details and updates!


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Thomas Sawyer  
View profile
 More options Jul 26, 5:44 pm
Newsgroups: comp.lang.ruby
From: Thomas Sawyer <transf...@gmail.com>
Date: Sat, 26 Jul 2008 16:44:57 -0500
Local: Sat, Jul 26 2008 5:44 pm
Subject: Re: head/tail split for path
Hi David,

David A. Black wrote:

> Check out the Pathname feature:

>    require 'pathname'

>    p = Pathname.new("a/b/c")

> You get p.basename and p.dirname. Both return Pathname objects, but
> they're quite string-like and easily converted.

    p = Pathname.new("a/b/c")

    p.dirname   #=> "a/b"
    p.basename  #=> "c"

But I need "a" and "b/c".

T.
--
Posted via http://www.ruby-forum.com/.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
David A. Black  
View profile
 More options Jul 26, 5:59 pm
Newsgroups: comp.lang.ruby
From: "David A. Black" <dbl...@rubypal.com>
Date: Sat, 26 Jul 2008 16:59:01 -0500
Local: Sat, Jul 26 2008 5:59 pm
Subject: Re: head/tail split for path
Hi --

Whoops. Well, you could do:

require 'enumerator'
[path.to_enum(:ascend).to_a[1], path.basename]

or something like:

path.scan(/([^\/]+)\/(.*)/)  # with the String path

Pathname#cleanpath might come in handy if you're rolling your own. So
might File::Separator.

David

--
Rails training from David A. Black and Ruby Power and Light:
     Intro to Ruby on Rails  July 21-24      Edison, NJ
  *  Advancing With Rails    August 18-21    Edison, NJ
  * Co-taught by D.A. Black and Erik Kastner
See http://www.rubypal.com for details and updates!


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bill Walton  
View profile
 More options Jul 27, 2:13 pm
Newsgroups: comp.lang.ruby
From: Bill Walton <bill.wal...@charter.net>
Date: Sun, 27 Jul 2008 13:13:55 -0500
Local: Sun, Jul 27 2008 2:13 pm
Subject: Re: head/tail split for path
Hi Trans,

Trans wrote:
> I need to split a path by head/*tail.

> Ex.

>     File.head_tail_split('home/foo/bar')  #=> [ 'home', 'foo/bar' ]

You could do like this.

def head_tail_split(fname)
    components = fname.split('/')
    [components.shirt, components.join('/')]
end

irb(main):001:0> fname = 'home/foo/bar'
=> "home/foo/bar"
irb(main):002:0> components = fname.split('/')
=> ["home", "foo", "bar"]
irb(main):003:0> [components.shift, components.join('/')]
=> ["home", "foo/bar"]

HTH,
Bill


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Phlip  
View profile
 More options Jul 27, 2:20 pm
Newsgroups: comp.lang.ruby
From: Phlip <phlip2...@gmail.com>
Date: Sun, 27 Jul 2008 11:20:18 -0700
Local: Sun, Jul 27 2008 2:20 pm
Subject: Re: head/tail split for path

Bill Walton wrote:
> You could do like this.

> def head_tail_split(fname)
>     components = fname.split('/')
>     [components.shirt, components.join('/')]
> end

> irb(main):001:0> fname = 'home/foo/bar'
> => "home/foo/bar"
> irb(main):002:0> components = fname.split('/')
> => ["home", "foo", "bar"]
> irb(main):003:0> [components.shift, components.join('/')]
> => ["home", "foo/bar"]

That would be safer if fname were a Pathname.

But then, would split only split off the last path part? Or the first?

If the last, how could we roll many splits up, then pop the first?

--
   Phlip


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Thomas Sawyer  
View profile
 More options Jul 27, 2:47 pm
Newsgroups: comp.lang.ruby
From: Thomas Sawyer <transf...@gmail.com>
Date: Sun, 27 Jul 2008 13:47:33 -0500
Local: Sun, Jul 27 2008 2:47 pm
Subject: Re: head/tail split for path

That's basically were I ended up too, but using David's File::Separator
suggestion.

However you made me think it would be helpful for Pathname to have:

  class Pathname
    def to_a
      to_s.split(File::Separator)  # better definition ?
    end
  end

The funny thing is that reminds me of a rewrite of Pathname I did a
while back that used an internal array instead of a string to store the
path. It was ~20% faster than the current lib. But alas, no one cared :(

T.
--
Posted via http://www.ruby-forum.com/.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
ara.t.howard  
View profile
 More options Jul 27, 11:27 pm
Newsgroups: comp.lang.ruby
From: "ara.t.howard" <ara.t.how...@gmail.com>
Date: Sun, 27 Jul 2008 22:27:20 -0500
Local: Sun, Jul 27 2008 11:27 pm
Subject: Re: head/tail split for path

On Jul 26, 2008, at 1:54 PM, Trans wrote:

cfp:~ > cat a.rb
path = File.join 'a', 'b', 'c'

head, tail = path.split( File::SEPARATOR, 2 )

p :head => head, :tail => tail

cfp:~ > ruby a.rb
{:tail=>"b/c", :head=>"a"}

a @ http://codeforpeople.com/
--
we can deny everything, except that we have the possibility of being  
better. simply reflect on that.
h.h. the 14th dalai lama


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bill Walton  
View profile
 More options Jul 27, 11:29 pm
Newsgroups: comp.lang.ruby
From: Bill Walton <bill.wal...@charter.net>
Date: Sun, 27 Jul 2008 22:29:06 -0500
Local: Sun, Jul 27 2008 11:29 pm
Subject: Re: head/tail split for path
Hi Phlip,

Phlip wrote:
> That would be safer if fname were a Pathname.

Not sure what you mean here by 'safer'.  Say more?

Best regards,
Bill


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Martin DeMello  
View profile
 More options Jul 28, 1:48 am
Newsgroups: comp.lang.ruby
From: Martin DeMello <martindeme...@gmail.com>
Date: Mon, 28 Jul 2008 00:48:26 -0500
Local: Mon, Jul 28 2008 1:48 am
Subject: Re: head/tail split for path

On Sun, Jul 27, 2008 at 11:47 AM, Thomas Sawyer <transf...@gmail.com> wrote:
>> irb(main):002:0> components = fname.split('/')
>> => ["home", "foo", "bar"]
>> irb(main):003:0> [components.shift, components.join('/')]
>> => ["home", "foo/bar"]

> That's basically were I ended up too, but using David's File::Separator
> suggestion.

Don't forget the second argument to split - it'd be handy here.

first, rest = fname.split(File::Separator, 2)

though you'd probably have to thrown in an
  if fname.index(File::Separator) == 0 then fname = fname[1..-1]
first

martin


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Phlip  
View profile
 More options Jul 28, 7:34 am
Newsgroups: comp.lang.ruby
From: Phlip <phlip2...@gmail.com>
Date: Mon, 28 Jul 2008 04:34:38 -0700
Local: Mon, Jul 28 2008 7:34 am
Subject: Re: head/tail split for path

Bill Walton wrote:
>> That would be safer if fname were a Pathname.

> Not sure what you mean here by 'safer'.  Say more?

At work we just finished a rewrite of a system that generously manipulated
folders, paths, and files. The old system originally used only strings, and
string surgery, to manipulate paths. (The old system was also very shabby and
patched up; it started as a one-shot script with no structure, etc.)

In the new system we follow a simple rule: If it's a filename, relative path, or
absolute path of any kind, it's a Pathname. This allows us to stay within the
Pathname feature set, and manipulate paths without any string surgery. The
result is much more typesafe.

In theory, Pathnames would be safer if you needed to support \ path delimiters,
and if you needed to support paths with embedded \ or / characters. We are very
good string surgeons, so we never had those problems.

--
   Phlip


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
David A. Black  
View profile
 More options Jul 28, 7:35 am
Newsgroups: comp.lang.ruby
From: "David A. Black" <dbl...@rubypal.com>
Date: Mon, 28 Jul 2008 06:35:02 -0500
Local: Mon, Jul 28 2008 7:35 am
Subject: Re: head/tail split for path
Hi --

Ironically, I tried that first and discarded it because I had
misunderstood Tom's question and thought he wanted dirname/basename,
and then didn't resurrect it when I finally understood :-) The only
tweak I might make would be to use Pathname#clean (or roll one's own)
in case there are consecutive separators.

David

--
Rails training from David A. Black and Ruby Power and Light:
  *  Advancing With Rails    August 18-21    Edison, NJ
  * Co-taught by D.A. Black and Erik Kastner
See http://www.rubypal.com for details and updates!


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
ara.t.howard