Mixin of class methods?

Discussion in 'Ruby' started by Michael Roth, Sep 24, 2005.

  1. Michael Roth

    Michael Roth Guest

    Hello all,

    I'm a little bit new to the wonderful world of ruby and have a small
    problem. I would like to mixin a module method:

    module M
    def M.foobar id
    puts "Hello Ruby: #{id}"
    end
    end

    class C
    include M
    foobar :example
    end

    But that doesn't work. I'm trying to write some helper class methods
    like attr_accessor and friends.

    I don't like to write

    class C
    include M
    M.foobar :example
    end

    because 'M' has to many chars to type...
    My helper class methods should look like attr_accessor and friends.

    What is the right way to solve this problem?


    Michael Roth
     
    Michael Roth, Sep 24, 2005
    #1
    1. Advertising

  2. module M
    def foobar(id)
    puts "Hello Ruby: #{id}"
    end
    end

    class C
    class << self
    include M
    end
    foobar :example
    end
     
    Kevin Ballard, Sep 24, 2005
    #2
    1. Advertising

  3. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    On Sep 24, 2005, at 1:56 PM, Michael Roth wrote:
    > I'm a little bit new to the wonderful world of ruby and have a small
    > problem. I would like to mixin a module method:
    >
    > module M
    > def M.foobar id
    > puts "Hello Ruby: #{id}"
    > end
    > end
    >
    > class C
    > include M
    > foobar :example
    > end


    module M
    def foobar id
    puts "Hello Ruby: #{id}"
    end
    end

    class C
    extend M
    foobar :example
    end

    OR if you'd like M to have both instance and class methods:

    module M
    def self.included(base)
    super
    base.extend ClassMethods
    end

    def baz
    # ...
    end

    module ClassMethods
    def foobar id
    # ...
    end
    end
    end

    class C
    include M
    foobar :example
    end

    C.new.baz

    Modules are pleasant little beasts.

    Best,
    jeremy
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.2 (Darwin)

    iD8DBQFDNcDfAQHALep9HFYRAsQNAKCG5eMkzpBIjNkc0Tk4qDcMI/UEHQCg1HF2
    rpdtYLov7fyKH9+SC24VU2c=
    =4RK9
    -----END PGP SIGNATURE-----
     
    Jeremy Kemper, Sep 24, 2005
    #3
  4. Michael Roth

    Michael Roth Guest

    Kevin Ballard wrote:

    > class C
    > class << self
    > include M
    > end
    > foobar :example
    > end


    Thank you very much.

    Hmm, the 'class <<' thing creates an singleton class. Looks like I
    should take some time to read some more pages from the ruby book...


    Michael Roth
     
    Michael Roth, Sep 24, 2005
    #4
  5. Michael Roth

    Michael Roth Guest

    Jeremy Kemper wrote:

    > class C
    > extend M
    > foobar :example
    > end


    This looks very nice. Thank you!


    > Modules are pleasant little beasts.


    Oh yes...


    Michael Roth
     
    Michael Roth, Sep 24, 2005
    #5
  6. Michael Roth

    Ara.T.Howard Guest

    On Sun, 25 Sep 2005, Michael Roth wrote:

    > Hello all,
    >
    > I'm a little bit new to the wonderful world of ruby and have a small
    > problem. I would like to mixin a module method:
    >
    > module M
    > def M.foobar id
    > puts "Hello Ruby: #{id}"
    > end
    > end
    >
    > class C
    > include M
    > foobar :example
    > end
    >
    > But that doesn't work. I'm trying to write some helper class methods
    > like attr_accessor and friends.
    >
    > I don't like to write
    >
    > class C
    > include M
    > M.foobar :example
    > end
    >
    > because 'M' has to many chars to type...
    > My helper class methods should look like attr_accessor and friends.
    >
    > What is the right way to solve this problem?


    i think this is clearest for people reading your code:

    module M
    module InstanceMethods
    end
    module ClassMethods
    end
    def self::included other
    other.module_eval{ include InstanceMethods }
    other.extend ClassMethods
    other
    end
    end

    class C
    include M
    end

    but it's an opinion...

    -a
    --
    ===============================================================================
    | email :: ara [dot] t [dot] howard [at] noaa [dot] gov
    | phone :: 303.497.6469
    | Your life dwells amoung the causes of death
    | Like a lamp standing in a strong breeze. --Nagarjuna
    ===============================================================================
     
    Ara.T.Howard, Sep 24, 2005
    #6
  7. Michael Roth

    michele Guest

    How about:

    class Module
    def foobar id
    puts "Hello Ruby: #{id}"
    end
    end

    class C
    foobar :example
    end

    my_c = C.new
     
    michele, Sep 25, 2005
    #7
  8. On 9/25/05, michele <> wrote:
    > How about:
    >
    > class Module
    > def foobar id
    > puts "Hello Ruby: #{id}"
    > end
    > end
    >
    > class C
    > foobar :example
    > end
    >
    > my_c =3D C.new
    >

    This is fine if you want to add the foobar method to ~all~ classes and
    modules. I'd recommend Ara's way if you want to be more selective
    about which classes get the methods.

    Sean
     
    Sean O'Halpin, Sep 25, 2005
    #8
  9. Michael Roth

    michele Guest

    > module M
    > module InstanceMethods
    > end
    > module ClassMethods
    > end
    > def self::included other
    > other.module_eval{ include InstanceMethods }
    > other.extend ClassMethods
    > other <------------------?
    > end
    > end
    >
    > class C
    > include M
    > end


    Why the "other" return (see question mark above)? It seems to work
    without.
     
    michele, Sep 26, 2005
    #9
    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. Florian Frank

    Extract methods in a class to mixin?

    Florian Frank, Sep 10, 2003, in forum: Ruby
    Replies:
    0
    Views:
    118
    Florian Frank
    Sep 10, 2003
  2. Kenneth McDonald
    Replies:
    5
    Views:
    378
    Kenneth McDonald
    Sep 26, 2008
  3. dc

    removing mixin methods

    dc, Oct 31, 2008, in forum: Ruby
    Replies:
    1
    Views:
    87
    Mike Gold
    Oct 31, 2008
  4. James Coglan
    Replies:
    4
    Views:
    114
    ara.t.howard
    Nov 15, 2008
  5. John Lane
    Replies:
    6
    Views:
    209
    John Lane
    Feb 9, 2010
Loading...

Share This Page