[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Class Methods vs Module Methods?

PHP HD

6/18/2007 3:54:00 PM

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?

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

2 Answers

Daniel Kempkens

6/18/2007 4:25:00 PM

0

PHP HD schrieb:
> 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!

Gavin Kistner

6/18/2007 4:34:00 PM

0

On Jun 18, 9:54 am, PHP HD <chuck....@merrillcorp.com> wrote:
> 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.