Forum: Ruby Class Methods vs Module Methods?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
PH P. (Guest)
on 2007-06-18 19:54
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?
Daniel K. (Guest)
on 2007-06-18 20:31
(Received via mailing list)
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 K. (Guest)
on 2007-06-18 20:36
(Received via mailing list)
On Jun 18, 9:54 am, PHP HD <removed_email_address@domain.invalid> 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.
This topic is locked and can not be replied to.