modules_as_namespaces_and_mixins
🧩 Organize Code with Modules as Namespaces and Mixins​
Modules in Ruby can serve both as namespaces to prevent constant collisions and as mixins to share behavior across classes. By structuring your modules hierarchically, you can keep your codebase tidy and avoid polluting the global scope.
Example of using modules as namespaces:
module PaymentGateway
module Stripe
class Client
def charge(amount)
# implementation
end
end
end
end
# Usage
target = PaymentGateway::Stripe::Client.new
Mixin behavior across classes:
module Trackable
def track(event)
puts "Tracking [32m[0m#{event}" # colorized output
end
end
class Order
include Trackable
end
order = Order.new
order.track('order_created')