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

Ruby on Rails controllers are an easy place to get code creep (a lot of code in a place where it shouldn’t be or unneeded code). The following is one simple way we have refactored some of our very large controllers for easy readability. The way most books show you. 1 2 3 4 5 6 7 8 9 class SearchController < ApplicationController   before_filter :initialize_search, :only => [:search_results] before_filter :verify_locations, :only => [:search_results] before_filter :ambiguous_locations, :only => [:search_results] before_filter :verify_time, :only => [:search_results] before_filter :grace_period, :only => [:search_results]   #… 122 123 124 125 126 127 128 129 130 131 132   #…   def search_results @search_results = run_search if @search_results.empty? @errors << ‘No results, try a new search’.t[…]