adding class-methods via modules

Discussion in 'Ruby' started by John Wilger, Nov 23, 2004.

  1. John Wilger

    John Wilger Guest

    Hello,

    I have a module which, when a class includes the module, I would like
    to have it add certain class-methods to the including class.
    Currently, I am doing the following:

    module Foo
    def self.included(mod)
    mod.class_eval <<-EOF, __FILE__, __LINE__+1
    def self.bar
    ...
    end
    EOF
    end
    end

    class Baz
    include Foo
    end

    This does accomplish the end goal---Baz::bar is a valid class
    method---but it has the downside that RDoc doesn't pick up the class
    methods defined inside Foo::included for documentation purposes.

    Is there either a) a way to get RDoc to pick up documentation for the
    ::bar method (and simply document it as Foo::bar), or b) a better way
    of creating these class methods on classes that include Foo that will
    also allow RDoc to document these methods?

    --
    Regards,
    John Wilger

    -----------
    Alice came to a fork in the road. "Which road do I take?" she asked.
    "Where do you want to go?" responded the Cheshire cat.
    "I don't know," Alice answered.
    "Then," said the cat, "it doesn't matter."
    - Lewis Carrol, Alice in Wonderland
     
    John Wilger, Nov 23, 2004
    #1
    1. Advertising

  2. "John Wilger" <> schrieb im Newsbeitrag
    news:...
    > Hello,
    >
    > I have a module which, when a class includes the module, I would like
    > to have it add certain class-methods to the including class.
    > Currently, I am doing the following:
    >
    > module Foo
    > def self.included(mod)
    > mod.class_eval <<-EOF, __FILE__, __LINE__+1
    > def self.bar
    > ...
    > end
    > EOF
    > end
    > end
    >
    > class Baz
    > include Foo
    > end
    >
    > This does accomplish the end goal---Baz::bar is a valid class
    > method---but it has the downside that RDoc doesn't pick up the class
    > methods defined inside Foo::included for documentation purposes.
    >
    > Is there either a) a way to get RDoc to pick up documentation for the
    > ::bar method (and simply document it as Foo::bar), or b) a better way
    > of creating these class methods on classes that include Foo that will
    > also allow RDoc to document these methods?


    Two options for b) come to mind:

    # option 1
    module Foo
    module FooClassMethods
    def bar() puts "#bar#" end
    end

    def self.included(mod)
    class <<mod
    include FooClassMethods
    end
    end
    end

    # option 2
    module Foo2
    def self.included(mod)
    class <<mod
    def bar2() puts "#bar2#" end
    end
    end
    end

    class Baz
    include Foo
    include Foo2
    end

    Baz.bar
    Baz.bar2

    Personally I'd prefer option 1, because you end up with less method
    definitions and if you change definitions in FooClassMethods later all
    classes that included Foo will use the new definitions.

    Kind regards

    robert
     
    Robert Klemme, Nov 24, 2004
    #2
    1. Advertising

  3. John Wilger

    John Wilger Guest

    Awesome! This is exactly why I love Ruby---not just the flexibility of
    the language, but also the helpfulness of the community. Thanks!

    --
    Regards,
    John Wilger

    -----------
    Alice came to a fork in the road. "Which road do I take?" she asked.
    "Where do you want to go?" responded the Cheshire cat.
    "I don't know," Alice answered.
    "Then," said the cat, "it doesn't matter."
    - Lewis Carrol, Alice in Wonderland
     
    John Wilger, Nov 24, 2004
    #3
  4. "John Wilger" <> schrieb im Newsbeitrag
    news:...
    > Awesome! This is exactly why I love Ruby---not just the flexibility of
    > the language, but also the helpfulness of the community. Thanks!


    You're welcome.

    Btw: option 1 can be written even more concise:

    module Foo
    module FooClassMethods
    def bar() puts "#bar#" end
    end

    def self.included(mod)
    mod.extend FooClassMethods
    end
    end

    class Baz
    include Foo
    end

    Baz.bar

    :))

    Kind regards

    robert
     
    Robert Klemme, Nov 24, 2004
    #4
    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:
    394
    Terry Reedy
    Mar 11, 2009
  2. Tim Bates
    Replies:
    3
    Views:
    108
    Joel VanderWerf
    Feb 3, 2004
  3. Geert Fannes

    adding methods to classes via C

    Geert Fannes, Jun 8, 2005, in forum: Ruby
    Replies:
    1
    Views:
    120
    Guillaume Cottenceau
    Jun 9, 2005
  4. Vincent Fourmond

    Modules, instance methods and class methods

    Vincent Fourmond, Sep 26, 2006, in forum: Ruby
    Replies:
    5
    Views:
    171
    Trans
    Sep 27, 2006
  5. Kenneth McDonald
    Replies:
    5
    Views:
    387
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page