Created
April 19, 2014 15:19
-
-
Save hisaichi5518/11087390 to your computer and use it in GitHub Desktop.
includeしただけで、クラスメソッドも生やしたい
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
| $ ruby t.rb | |
| {:push=>[#<Proc:[email protected]:27>]} | |
| {:push=>[#<Proc:[email protected]:27>, #<Proc:[email protected]:29>]} | |
| {:push=>[#<Proc:[email protected]:32>]} | |
| {:push=>[#<Proc:[email protected]:32>, #<Proc:[email protected]:34>]} | |
| {:push=>[#<Proc:[email protected]:37>]} | |
| {:push=>[#<Proc:[email protected]:37>, #<Proc:[email protected]:39>]} |
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
| module Tiger | |
| module ClassMethods | |
| def on(name, &code) | |
| @triggers ||= {} | |
| @triggers[name] ||= [] | |
| @triggers[name].push(code) | |
| p @triggers | |
| end | |
| end | |
| include ClassMethods | |
| def self.included(klass) | |
| klass.extend ClassMethods | |
| end | |
| end | |
| class TestTigerClass | |
| include Tiger | |
| end | |
| module TestTigerModule | |
| include Tiger | |
| end | |
| test_tiger_class = TestTigerClass.new | |
| test_tiger_class.on(:push) do | |
| end | |
| test_tiger_class.on(:push) do | |
| end | |
| TestTigerClass.on(:push) do | |
| end | |
| TestTigerClass.on(:push) do | |
| end | |
| TestTigerModule.on(:push) do | |
| end | |
| TestTigerModule.on(:push) do | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment