Net :: ftp getbinaryfile()保存到文件与保存到变量

use*_*677 6 ruby default-parameters net-ftp

使用以下ftp_download方法有效,但如果我改变

ftp.getbinaryfile(file,localdir,1024)   #=> Saves the file to localdir
Run Code Online (Sandbox Code Playgroud)

ftp.getbinaryfile(file)    #=> returns nil
Run Code Online (Sandbox Code Playgroud)

nil回来了.根据

http://www.ruby-doc.org/stdlib-2.0/libdoc/net/ftp/rdoc/Net/FTP.html#method-i-getbinaryfile

inilf我设置localfilenil如上述,数据应被检索以及由该方法返回.我究竟做错了什么?

def ftp_download(domain,remotedir,filename_regex,user=nil,pwd=nil)
  ftp = Net::FTP::new(domain)
  if user && pwd
    ftp.login(user, pwd)
  end
  ftp.chdir(remotedir)
  fileList = ftp.nlst(filename_regex)

  fileList.each do |file|
    localdir=File.join(remotedir,file)
    localdir=localdir[1..-1] if localdir[0]="/"
    FileUtils.mkdir_p(File.dirname(localdir))
    ftp.getbinaryfile(file,localdir,1024)
  end
  ftp.close
end
Run Code Online (Sandbox Code Playgroud)

tor*_*o2k 11

如果你看一下getbinaryfile方法签名,你会发现,第二个参数(默认值localfile)不是nil,但File.basename(remotefile)

getbinaryfile(remotefile, 
              localfile=File.basename(remotefile), 
              blocksize=DEFAULT_BLOCKSIZE)
Run Code Online (Sandbox Code Playgroud)

如果你想localfile成为nil必须明确传递它:

ftp.getbinaryfile(file, nil)
Run Code Online (Sandbox Code Playgroud)