When to use a module, and when to use a class
Posted
by Matt Briggs
on Stack Overflow
See other posts from Stack Overflow
or by Matt Briggs
Published on 2010-04-19T23:13:01Z
Indexed on
2010/04/19
23:43 UTC
Read the original article
Hit count: 318
I am currently working through the Gregory Brown Ruby Best Practices book. Early on, he is talking about refactoring some functionality from helper methods on a related class, to some methods on module, then had the module extend self
.
Hadn't seen that before, after a quick google, found out that extend self
on a module lets methods defined on the module see each other, which makes sense.
Now, my question is when would you do something like this
module StyleParser
extend self
def process(text)
...
end
def style_tag?(text)
...
end
end
and then refer to it in tests with
@parser = Prawn::Document::Text::StyleParser
as opposed to just using a class with some class methods on it? is it so that you can use it as a mixin? or are there other reasons I'm not seeing?
© Stack Overflow or respective owner