Can class methods be in a module?

Is it possible to put a class method in a module, for example:

module FtpClientMod
def self.do_send(file_template, show_progress)

and include it in a class as follows?

require ‘ftpclientmod’

class FtpClient
include FtpClientMod

When I try this I get an error that the method cannot be found.

On 10/21/07, Bazsl [email protected] wrote:

include FtpClientMod

When I try this I get an error that the method cannot be found.

http://www.ruby-doc.org/core/classes/Object.html#M000337

Michael F. wrote:

I guess I was not clear. I am trying to place a class method in a module
and include it in the class. I am not trying to instantiate an instance
of a class and and include the method from the module in the instance,
which, if I am reading your reference correctly, is what extend does.
Note that I am very new to Ruby so I may be missing something. Thanks.

Bazsl wrote:

Is it possible to put a class method in a module, for example:

module FtpClientMod
def self.do_send(file_template, show_progress)

and include it in a class as follows?

require ‘ftpclientmod’

class FtpClient
include FtpClientMod

When I try this I get an error that the method cannot be found.

Maybe this will work for you:

module MyMethods
def greet
puts “hello”
end
end

class Dog
include MyMethods
extend MyMethods
end

Dog.greet #hello

module FtpClientMod
def do_send(file_template, show_progress)
puts “do_send #{file_template}”
end
end

class FtpClient
class << self
include FtpClientMod
end
end

FtpClient.do_send(‘foo’, 0)

tho_mica_l wrote:

end

FtpClient.do_send(‘foo’, 0)

Thanks. That works but the method in the module does not have access to
class variables. Is there any way to give the method in the module
access to class variables?

7stud – wrote:

def greet
Dog.greet #hello

That works but the method in the module does not have access to class
variables. Is there any way to give the method in the module access to
class variables?

Thanks. That works but the method in the module does not have access to
class variables. Is there any way to give the method in the module
access to class variables?

Which class variable do you mean?

module FtpClientMod
@foo = 1
@@bar = 1
def do_send(file_template, show_progress)
puts “do_send #{file_template} #@foo #@@bar #{gimme_bar}”
end
def gimme_bar
@@bar
end
end

class FtpClient
@foo = 2
@@bar = 2
class << self
@foo = 3
include FtpClientMod

    def gimme_bar
        @@bar
    end
end

end

FtpClient.do_send(‘foo’, 0)
=> do_send foo 2 1 2

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs