Skip to content

Instantly share code, notes, and snippets.

@nnabeyang
Created July 25, 2012 12:50
Show Gist options
  • Select an option

  • Save nnabeyang/3176015 to your computer and use it in GitHub Desktop.

Select an option

Save nnabeyang/3176015 to your computer and use it in GitHub Desktop.
複数のモジュールにあるStaticeMethodsを1つのクラスのクラスメソッドにまとめる
module Concern
def self.extended(base)
base.instance_variable_set("@dependencies", [])
end
def append_features(base)
if base.instance_variable_defined?("@dependencies")
base.instance_variable_get("@dependencies") << self
else
super
@dependencies.each {|dep| base.send(:include, dep)}
base.extend const_get("ClassMethods") if const_defined?("ClassMethods")
end
end
end
module M
extend Concern
module ClassMethods
def m1
puts "hello"
end
end
end
module M2
extend Concern
include M
module ClassMethods
def m2
m1
end
end
end
class C
include M2
end
C.m2#=> hello
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment