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

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

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

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. E11
    Replies:
    1
    Views:
    5,664
    Thomas Weidenfeller
    Oct 12, 2005
  2. Maric Michaud
    Replies:
    0
    Views:
    7,331
    Maric Michaud
    Jun 24, 2006
  3. Oltmans
    Replies:
    6
    Views:
    615
    Terry Reedy
    Mar 11, 2009
  4. gga
    Replies:
    3
    Views:
    300
    William Morgan
    Feb 19, 2005
  5. Geert Fannes
    Replies:
    1
    Views:
    264
    David A. Black
    Apr 4, 2005
  6. Nikita Petrov
    Replies:
    2
    Views:
    246
    Gary Wright
    Apr 6, 2008
  7. Kenneth McDonald
    Replies:
    5
    Views:
    634
    Kenneth McDonald
    Sep 26, 2008
  8. John Lane
    Replies:
    6
    Views:
    332
    John Lane
    Feb 9, 2010
Loading...