In Ruby, Module mixins are a great way to keep code decoupled and organized. Here an example setup to include a module in your application.

 
class Course
 
  def initialize(type)
    add_calculator
  end
 
  def add_calculator
    @calculators = []
    ::Calculators.constants.each do |name|
      @calculators << ::Calculators.const_get(name).new(self)
    end
    puts @calculators
  end
 
 
end
 
module Calculators
 
  class Scientific
    def initialize(calculation)
      calculation.ui.extend(UiScientific)
    end 
  end 
 
  class Business
    def initialize(calculation)
      calculation.ui.extend(UiBusiness)
    end 
 
    module UiBusiness
      def compute(*)
        # some code that computes
        puts "Answer" 
        super
      end 
    end 
  end 
end

Leave a Reply

Your email address will not be published. Required fields are marked *