unless defined?(Shared) module Shared Shared::VERSION = '1.0.0' unless defined?(Shared::VERSION) def version() Shared::VERSION end Code = {} def shared name, &block key = key_for name return Code[key] if block.nil? m = (Code[key] || Module.new) singleton_class(m) do unless m.respond_to?(:blocks) blocks = [] define_method(:blocks){ blocks } define_method(:included) do |other| blocks.each{|b| other.send(:module_eval, &b)} end define_method(:extend_object) do |other| Shared.singleton_class(other) do m.blocks.each{|b| module_eval &b} end end end end m.blocks << block Code[key] ||= m end alias_method 'share', 'shared' alias_method 'for', 'shared' alias_method '[]', 'shared' def key_for name name.to_s.strip.downcase end def singleton_class object, &block singleton_class = class << object self end block ? singleton_class.module_eval(&block) : singleton_class end extend self end module Kernel private def share(*a, &b) Shared.share(*a, &b) end def Share(*a, &b) Shared.share(*a, &b) end def shared(*a, &b) Shared.shared(*a, &b) end def Shared(*a, &b) Shared.shared(*a, &b) end end end