aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorshugo <shugo@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2005-10-15 14:54:03 +0000
committershugo <shugo@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2005-10-15 14:54:03 +0000
commit65867866a870be8fbef90d67bd0fef8776aee09e (patch)
tree6b9c29fb790853fbef6f1bdcf85a462fb97d9ae2 /lib
parent190909c0ce92dde386ca7c7584447870d2fdd67a (diff)
downloadruby-65867866a870be8fbef90d67bd0fef8776aee09e.tar.gz
* lib/net/ftp.rb: (getbinaryfile): allow nil for localfile, and
returns retrieved data if localfile is nil. * lib/net/ftp.rb: (gettextfile): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9394 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r--lib/net/ftp.rb51
1 files changed, 34 insertions, 17 deletions
diff --git a/lib/net/ftp.rb b/lib/net/ftp.rb
index 20dc4eeec1..fe244f0775 100644
--- a/lib/net/ftp.rb
+++ b/lib/net/ftp.rb
@@ -507,43 +507,60 @@ module Net
#
# Retrieves +remotefile+ in binary mode, storing the result in +localfile+.
+ # If +localfile+ is nil, returns retrieved data.
# If a block is supplied, it is passed the retrieved data in +blocksize+
# chunks.
#
def getbinaryfile(remotefile, localfile = File.basename(remotefile),
- blocksize = DEFAULT_BLOCKSIZE, &block) # :yield: data
- if @resume
- rest_offset = File.size?(localfile)
- f = open(localfile, "a")
- else
- rest_offset = nil
- f = open(localfile, "w")
+ blocksize = DEFAULT_BLOCKSIZE) # :yield: data
+ result = nil
+ if localfile
+ if @resume
+ rest_offset = File.size?(localfile)
+ f = open(localfile, "a")
+ else
+ rest_offset = nil
+ f = open(localfile, "w")
+ end
+ elsif !block_given?
+ result = ""
end
begin
- f.binmode
+ f.binmode if localfile
retrbinary("RETR " + remotefile, blocksize, rest_offset) do |data|
- f.write(data)
- yield(data) if block
+ f.write(data) if localfile
+ yield(data) if block_given?
+ result.concat(data) if result
end
+ return result
ensure
- f.close
+ f.close if localfile
end
end
#
# Retrieves +remotefile+ in ASCII (text) mode, storing the result in
- # +localfile+. If a block is supplied, it is passed the retrieved data one
+ # +localfile+.
+ # If +localfile+ is nil, returns retrieved data.
+ # If a block is supplied, it is passed the retrieved data one
# line at a time.
#
- def gettextfile(remotefile, localfile = File.basename(remotefile), &block) # :yield: line
- f = open(localfile, "w")
+ def gettextfile(remotefile, localfile = File.basename(remotefile)) # :yield: line
+ result = nil
+ if localfile
+ f = open(localfile, "w")
+ elsif !block_given?
+ result = ""
+ end
begin
retrlines("RETR " + remotefile) do |line|
- f.puts(line)
- yield(line) if block
+ f.puts(line) if localfile
+ yield(line) if block_given?
+ result.concat(line + "\n") if result
end
+ return result
ensure
- f.close
+ f.close if localfile
end
end