Bill Walton wrote:
> 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"]

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/.