I've been researching topics related to code reuse and object oriented design in Ruby apps recently.
There are three popular ways of reusing code in the Ruby community:
class User < ActiveRecord::Base def change_login(new_login) login = new_login save end end
module Buyer def add_to_cart(product_id, quantity) cart.add(find_product(product_id), quantity)) end end class User include Buyer end #usage User.new.add_to_cart(params[:product_id, params[:quantity])
class User attr_accessor :buyer def add_to_cart(product_id, quantity) buyer.add_to_cart(product_id, quantity) end end
Update: Added code examples, changed modules to modules/mixins