[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

adding class-methods via modules

John Wilger

11/23/2004 4:50:00 PM

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


3 Answers

Robert Klemme

11/24/2004 9:18:00 AM

0


"John Wilger" <johnwilger@gmail.com> schrieb im Newsbeitrag
news:a05e51b6041123084753f0aa8@mail.gmail.com...
> 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

John Wilger

11/24/2004 1:30:00 PM

0

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


Robert Klemme

11/24/2004 2:01:00 PM

0


"John Wilger" <johnwilger@gmail.com> schrieb im Newsbeitrag
news:a05e51b6041124052979981b88@mail.gmail.com...
> 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