Question

This code logs in, and tries to change the directory, and then list the files in the directory:

Net::FTP.open(DOMAIN_NAME, LOGIN, PASSWORD) do |ftp|
  files = ftp.chdir("forms/")      
  puts files.list
end

I get this error when I run it:

undefined method `list' for nil:NilClass

The folder definitely exists. I saw it when I tried connecting using cyberduck, and if I do ftp.list.

What am I doing wrong?

Also how would I download a CSV file from the FTP server and then open it?

I tried reading the docs on rubylang and am quite stuck. If anyone could help that would be great!

Was it helpful?

Solution

chdir changes the directory. If you look at the underlying source, it doesn't return anything.

list returns the list of files:

Returns an array of file information in the directory (the output is like ls -l). If a block is given, it iterates through the listing.

As a result files will be nil, because chdir didn't return anything. Instead you need to do something like:

ftp.chdir('forms/')
files = ftp.list

Look at Example #2 in the docs for an example, and ignore that they try to assign the result of chdir.

nlist would be even better than list if all you want is the list of names. Use list if you want a long-listing of the files so you can parse out permissions, sizes, etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top