Can class methods be in a module?

B

Bazsl

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.
 
7

7stud --

Bazsl said:
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
 
T

tho_mica_l

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)
 
B

Bazsl

7stud said:
Maybe this will work for you:


module MyMethods
def greet
puts "hello"
end
end


class Dog
include MyMethods
extend MyMethods
end

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?
 
B

Bazsl

tho_mica_l said:
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)
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?
 
T

tho_mica_l

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,120
Latest member
ShelaWalli
Top