Question: I'm trying to crawl FTP and pull down all the files recursively. 

Up until now I was trying to pull down a directory with 

```
   ftp.list.each do |entry|
    if entry.split(/\s+/)[0][0, 1] == "d"
      out[:dirs] << entry.split.last unless black_dirs.include? entry.split.last
    else
      out[:files] << entry.split.last unless black_files.include? entry.split.last
    end

```

But turns out, if you split the list up until last space, filenames and directories with spaces are fetched wrong.
Need a little help on the logic here.

Answer: 
You can also use a regular expression. I put one together. Please verify if it works for you as well as I don't know it your dir listing look different. You have to use Ruby 1.9 btw.

```
reg = /^(?<type>.{1})(?<mode>\S+)\s+(?<number>\d+)\s+(?<owner>\S+)\s+(?<group>\S+)\s+(?<size>\d+)\s+(?<mod_time>.{12})\s+(?<path>.+)$/

match = entry.match(reg)

```

You are able to access the elements by name then

`match[:type]` contains a `'d'` if it's a directory, a space if it's a file.

All the other elements are there as well. Most importantly `match[:path]`.
Assuming that the FTP server returns *Unix-like* file listings, the following code works. At least for me.

```
regex = /^d[r|w|x|-]+\s+[0-9]\s+\S+\s+\S+\s+\d+\s+\w+\s+\d+\s+[\d|:]+\s(.+)/
ftp.ls.each do |line|
    if dir = line.match(regex)
        puts dir[1]
    end
end

```

`dir[1]` contains the name of the directory (given that the inspected line actually represents a directory).