Corrections in a Class

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

--
Posted via http://www.ruby-forum.com/.

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

···

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

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.

<snip>

public def connectViaFTP

You don't need to put "public" before your method, especially if you want to make it a singleton method. public is already the default access type.

To make this a singleton method, simply put either the name of the class, or self before it:

def self.connectViaFTP
...
end

or

def EstablishRemoteConnection.connectViaFTP
..
end

and there's also this interesting format:

class EstablishRemoteConnection
  class << self
     def connectViaFTP
         ..
     end
  end
end

These all have the same result of being able to do this:

EstablishRemoteConnection.connectViaFTP

Also as a side note Ruby naming conventions are generally in the form:

connect_ftp

without any sort of camel casing or such. Class/Module names are the ones that tend to utilize camel case. Whatever works for you though. Though you might also want to keep your class names a bit shorter:

RemoteConnection.ftp_connection

Regards,
Chris White
Twitter: http://www.twitter.com/cwgem

···

On Aug 4, 2011, at 9:05 AM, Rubyist Rohit wrote:

With this what I followed is:

-- that in Ruby, methods are by default public. If a method scope is
private, then Self is to be used.

-- Instance variables are prefixed with @

If a method can be called by another inherited class but I want to
protect it's scope as Public, then which keyword to use?

···

--
Posted via http://www.ruby-forum.com/.

Hi, What about this code?

     require 'net/ftp'

     class RemoteConnection
       #Constants
       ServerName = "my FTP Server URL"
       UserName = "My Username"
       Password = "My Password"
       FilePath = "My remote Sub Folder"

       def self.connect_via_ftp
         #FTP instance
         @ftp = Net::FTP.new
         @ftp.connect(ServerName, 21)
         @ftp.login(UserName, Password)
         @ftp.chdir(FilePath)
         #@ftp.getbinaryfile(filename)
       end

       def self.disconnect_ftp
         if @ftp
           @ftp.close
           @ftp = nil
         end
       end
     end

     # USAGE
     RemoteConnection.connect_via_ftp
     RemoteConnection.disconnect_ftp

···

On 08/04/2011 07:05 PM, Rubyist Rohit wrote:

I also want to improve the way it is written.

Looks fine but the instance:

@ftp = Net::FTP.new

is declared in the method: connect_via_ftp. Will it be accessible in the
method: disconnect_ftp?

···

--
Posted via http://www.ruby-forum.com/.

-- that in Ruby, methods are by default public.

Yes.

If a method scope is
private, then Self is to be used.

No. def self.foo gives you a 'static' method, ie, a method defined on
the class of the object rather than on the object itself.

-- Instance variables are prefixed with @

yes.

If a method can be called by another inherited class but I want to
protect it's scope as Public, then which keyword to use?

I'm not sure what you're asking. "protect its scope as public"?

Yes. Variables with an @ are 'instance variables', and so are in scope
anywhere inside an instance of the class.

If a method can be called by another inherited class but I want to
protect it's scope as Public, then which keyword to use?

Steve Klabnik wrote in post #1014980:

I'm not sure what you're asking. "protect its scope as public"?

Let me clear. I want to permit the use of a method to only inherited
classes but not by other classes. This method should be, something like
protected.

···

--
Posted via http://www.ruby-forum.com/\.

Let me clear. I want to permit the use of a method to only inherited
classes but not by other classes. This method should be, something like
protected.

Protected exists in Ruby. Just call 'protected'

class Foo
  def this_method_is_public; end;

  protected

  def this_method_is_protected; end;

  private

  def this_method_is_private; end
end