Hi --

On Sun, 27 Jul 2008, 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?

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!