Background:
I have a module which declares a number of instance methods
module UsefulThings def get_file; ... def delete_file; ... def format_text(x); ... end And I want to call some of these methods from within a class. How you normally do this in ruby is like this:
class UsefulWorker include UsefulThings def do_work format_text("abc") ... end end Problem
include UsefulThings brings in all of the methods from UsefulThings. In this case I only want format_text and explicitly do not want get_file and delete_file.
I can see several possible solutions to this:
- Somehow invoke the method directly on the module without including it anywhere
- I don't know how/if this can be done. (Hence this question)
- Somehow include
Usefulthingsand only bring in some of it's methods- I also don't know how/if this can be done
- Create a proxy class, include
UsefulThingsin that, then delegateformat_textto that proxy instance- This would work, but anonymous proxy classes are a hack. Yuck.
- Split up the module into 2 or more smaller modules
- This would also work, and is probably the best solution I can think of, but I'd prefer to avoid it as I'd end up with a proliferation of dozens and dozens of modules - managing this would be burdensome
Why are there lots of unrelated functions in a single module? It's ApplicationHelper from a rails app, which our team has de-facto decided on as the dumping ground for anything not specific enough to belong anywhere else. Mostly standalone utility methods that get used everywhere. I could break it up into seperate helpers, but there'd be 30 of them, all with 1 method each... this seems unproductive
Module#includedcallback to trigger anincludeof the other. Theformat_textmethod could be moved into it's own module, since it seems to be useful on it's own. This would make management a little less burdensome.module UT; def add1; self+1; end; def add2; self+2; end; endand you want to useadd1but notadd2in classFixnum. How would it help to have module functions for that? Am I missing something?