Last active
March 8, 2018 10:31
-
-
Save externvoid/953c2ec16f12f7aa39060764d9f5ee32 to your computer and use it in GitHub Desktop.
DSLの作り方
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # クラスメソッド Enalbe_foo | |
| module Product | |
| def foo | |
| puts "OK2" | |
| end | |
| end | |
| class F | |
| include Product | |
| end | |
| class Object | |
| def acts_as_product | |
| #def self.acts_as_product | |
| include Product | |
| end | |
| end | |
| class G | |
| acts_as_product | |
| end | |
| class A < G | |
| end | |
| A.new.foo | |
| # | |
| module Foo | |
| def foo | |
| puts "OK foo" | |
| end | |
| end | |
| module Enalbe_foo | |
| module ClassMethods | |
| def enable_foo | |
| include Foo | |
| end | |
| end | |
| def self.included(base) | |
| base.extend(ClassMethods) # making class method | |
| end | |
| end | |
| # この手法のメリットは? | |
| Object.instance_eval{include Enalbe_foo} | |
| class H | |
| enable_foo # calling class method | |
| end | |
| class B < H | |
| end | |
| B.new.foo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment