This page contains automated test results for code from O'Reilly's Ruby Cookbook. If this code looks interesting or useful, you might want to buy the whole book.

Being an FTP Client
CodeExpectedActual
require 'net/ftp'
ftp = Net::FTP.open('ftp.ibiblio.org') do |ftp|
  ftp.login
  ftp.chdir('pub/linux/')
  ftp.list('*Linux*') { |file| puts file }
  puts
  puts 'Saving a text file to disk while processing it.'
  ftp.gettextfile('How-do-I-get-Linux') { |line| puts "! #{line}" }
  puts "Saved #{File.size 'How-do-I-get-Linux'} bytes."
  puts
  puts 'Saving a binary file to disk.'
  ftp.getbinaryfile('INDEX.whole.gz')  
 puts "Saved #{File.size 'INDEX.whole.gz'} bytes."
end
-rw-r--r--   1 (?)      users    16979001 Jan  1 11:31 00-find.Linux.gz
-rw-rw-r--   1 (?) admin          73 Mar  9  2001 How-do-I-get-Linux
Saving a text file to disk while processing it.
!
!   Browse to http://metalab.unc.edu/linux/HOWTO/Installation-HOWTO.html
!
Saved 73 bytes.
Saving a binary file to disk.
Saved 213507 bytes.
-rw-r--r--   1 (?)      users      475798 Apr 12 10:01 00-find.Linux.gz
-rw-rw-r--   1 (?)      admin          73 Mar  9  2001 How-do-I-get-Linux

Saving a text file to disk while processing it.
! 
!   Browse to http://metalab.unc.edu/linux/HOWTO/Installation-HOWTO.html
! 
Saved 73 bytes.

Saving a binary file to disk.
Saved 213906 bytes.
ftp.login('leonardr', 'mypass')
class Net::FTP
  def processtextfile(remotefile)
    retrlines('RETR ' + remotefile) { |line| yield line }
  end
  def processbinaryfile(remotefile, blocksize=DEFAULT_BLOCKSIZE)
    retrbinary('RETR ' + remotefile, blocksize) { |data| yield data }
  end
end
require 'net/ftp'
Net::FTP.open('myisp.example.com') do |ftp|
  ftp.login('leonardr', 'mypass')
  ftp.chdir('public_html')
  ftp.puttextfile('index.html')
end
...
NoMethodError: undefined method `login' for nil:NilClass
	from (irb):15
SocketError: getaddrinfo: Name or service not known
	from /usr/lib/ruby/1.8/net/ftp.rb:159:in `initialize'
	from /usr/lib/ruby/1.8/net/ftp.rb:159:in `open_socket'
	from /usr/lib/ruby/1.8/net/ftp.rb:175:in `connect'
	from /usr/lib/ruby/1.8/monitor.rb:229:in `synchronize'
	from /usr/lib/ruby/1.8/net/ftp.rb:174:in `connect'
	from /usr/lib/ruby/1.8/net/ftp.rb:136:in `initialize'
	from /usr/lib/ruby/1.8/net/ftp.rb:113:in `open'
	from (irb):25