From: Stefano Crocco Date: 2011-08-05T01:19:11+09:00 Subject: Re: Corrections in a Class On Friday 05 August 2011 01:05:44 Rubyist Rohit wrote: > Below is the code of my initial Class in the Ruby world. I want to make > this Class a static class so that I can directly call methods, > connectViaFTP and disconnectFTP. > > I also want to improve the way it is written. > > ================================================== > require 'net/ftp' > > class EstablishRemoteConnection > > #Constants > ServerName = "my FTP Server URL" > UserName = "My Username" > Password = "My Password" > FilePath = "My remote Sub Folder" > > #FTP instance > ftp=Net::FTP.new > > public def connectViaFTP > > ftp.connect(ServerName,21) > ftp.login(UserName,Password) > ftp.chdir(FilePath) > #ftp.getbinaryfile(filename) > end > > public def disconnectFTP > ftp.close > end > end > ================================================= 1. remove the calls to public. By default methods are public, so they're not needed. 2. if you want to be able to write EstablishRemoteConnection.connectViaFTP and EstablishRemoteConnection.disconnectViaFTP, you need to make those methods class methods. To do this, instead of writing def connectViaFTP write def self.connectViaFTP 3. Your code won't work because the FTP instance is a local variable, which will have long disappeared when connectViaFTP or disconnectFTP will be called. You need either to make it a constant, calling it Ftp rather than ftp, or make it a class instance variable (that is, an instance variable of class EstablishRemoteConnection): @ftp = Net::FTP.new I hope this helps Stefano