Class Methods vs Module Methods?

Discussion in 'Ruby' started by PHP HD, Jun 18, 2007.

  1. PHP HD

    PHP HD Guest

    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-forum.com/.
    PHP HD, Jun 18, 2007
    #1
    1. Advertising

  2. 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!
    Daniel Kempkens, Jun 18, 2007
    #2
    1. Advertising

  3. PHP HD

    Phrogz Guest

    On Jun 18, 9:54 am, PHP HD <> 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.
    Phrogz, Jun 18, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Oltmans
    Replies:
    6
    Views:
    324
    Terry Reedy
    Mar 11, 2009
  2. Geert Fannes
    Replies:
    1
    Views:
    184
    David A. Black
    Apr 4, 2005
  3. Nikita Petrov
    Replies:
    2
    Views:
    101
    Gary Wright
    Apr 6, 2008
  4. Kenneth McDonald
    Replies:
    5
    Views:
    297
    Kenneth McDonald
    Sep 26, 2008
  5. John Lane
    Replies:
    6
    Views:
    174
    John Lane
    Feb 9, 2010
Loading...

Share This Page