-
2 weeks 2 days
-
2 weeks 4 days
-
4 weeks 2 days
-
4 weeks 5 days
-
4 weeks 6 days
FTP? nah, more like remote navigator.
I was checking out the ftp library of ruby and since the commands are minimal, I figured it would be fun to create a simple ftp program in one file. Since I'm feeling lazy, it ended-up as a simple remote directory navigator instead.
require 'net/ftp'
commands = ['ls', 'cd', 'quit']
response = ''
Net::FTP.open('ftp.mysite.com', 'ftpusername', 'ftppassword') do |ftp|
until response == :quit
print ">> "
response = gets.chomp
args = response.split(' ')
response = args[0]
#check for valid commands
unless commands.include?(response)
puts "\nInvalid command"
puts "Usage: ls, cd , size \n\n"
next
end
#if user want to quit
unless response != 'quit'
response = :quit
next
end
#respond accordingly
case response
when 'ls'
files = ftp.nlst('.')
unless files.empty?
files.each {|file| puts file}
end
when 'cd'
unless args[1]
puts "\nWrong number of arguments"
puts "Usage: cd \n\n"
next
end
ftp.chdir(args[1])
puts "Directory changed"
end
end
end
I think the code is short and straight forward but I'll explain it roughly. We pass a code block to the open method of the FTP class and inside the code block, we have a loop that keeps on asking for commands. If the user enters quit, we break out of the loop. The nice thing on calling the open method instead of the new is that it closes the ftp connection right after the code block ends.

Facebook
Twitter
I might upgrade this someday
I might upgrade this someday to a full pledge FTP program and since I'm into FXRuby, I'll make it fancy by adding a GUI. Hmm, lets see..
Cool. Yeah, more like remote
Cool. Yeah, more like remote navigator.Hmm, I think should start coding and learning new things. This post gives me motivation to do so.
Nice, I'll look forward
Nice, I'll look forward too! Btw, just added you on my links. Cheers!
Post new comment