module Shared Shared::VERSION = 0.4.3 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 defined?(@@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' 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 shared *a, &b Shared.shared *a, &b end end