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

> 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



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