I'm currently working with some code that IMO has abused ruby's mixin features. Given that I'm new to ruby, I wonder if MO in IMO is correct or not.
My primary question is what makes more sense (and if there are any major technical differences between them):
E.g.
1
to create Helper singleton classes (or Modules with class methods) to keep all my "helper" functions.
module Helper
def foo(); p 'foo'; end # instance method
end
class User
include Helper # foo() is now a method of User
def initialize()
foo() # included, invoked on User class object
end
end
------- OR -------
2
create mixin modules that contain these functions and include them everywhere I need them.
module Helper
def self.foo(); p 'foo'; end # class method
end
class User
def initialize()
Helper.foo() # Invoked on Helper module object
end
end
An example of the way it's abused in my case:
module ComplexMathPerformer
def perform_complex_math(); p 'perform_complex_math()'; end
end
module Renderer
def render_3d_object()
perform_complex_math() # from ComplexMathPerformer, which is NOT included here.
end
end
module Game
include ComplexMathPerformer
include Renderer
def play()
render_3d_object()
end
end
This code works because Game includes both Renderer and ComplexMathPerformer. But is a nightmare for someone who's looking at ComplexMathPerformer and trying to figure out where the hell is is foo() defined and how is it accessible in ComplexMathPerformer.
Logically it makes more sense to include ComplexMathPerformer but that's skipped because Game already includes ComplexMathPerformer (for it's own purposes). Making it a mess.