Class Methods vs Module Methods?

P

PHP HD

Hello all,

Is there anybody else confused about module methods? Why would you
create one when you could create a class method that is essentially the
same thing but with more functionality?
 
D

Daniel Kempkens

PHP said:
Hello all,

Is there anybody else confused about module methods? Why would you
create one when you could create a class method that is essentially the
same thing but with more functionality?
Because you can "mixin" these methods in classes.
module Foo
def test
puts "Hello!"
end
end

class Bar
include Foo
end

f = Bar.new
f.test #=> Hello!
 
P

Phrogz

Is there anybody else confused about module methods? Why would you
create one when you could create a class method that is essentially the
same thing but with more functionality?

Because sometimes you want a namespace for methods, but there's no
class that can be instantiated:

module Cupid
def self.createConnection( person1, person2 )
# ...mysterious details here...
end
end

Cupid.createConnection( ... )


These may also be utility methods used by 'instance' methods in a
module that will be mixed into one or more other classes.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top