Created
October 18, 2015 10:12
-
-
Save osyo-manga/dcd5d6afbb45a6fd5a93 to your computer and use it in GitHub Desktop.
concept
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
| class X | |
| def self.concept name, &request | |
| impl_name = "#{name}_#{request}" | |
| alias_method impl_name, name | |
| @@concept_methods ||= {} | |
| @@concept_methods[name] ||= [] | |
| @@concept_methods[name].unshift [request, impl_name] | |
| define_method(name){ |*args| | |
| for request, name_ in @@concept_methods[name] | |
| if request.(*args) | |
| return __send__ name_, *args | |
| end | |
| end | |
| } | |
| impl_name | |
| end | |
| def plus a, b | |
| p "plus by +" | |
| a + b | |
| end | |
| concept :plus do |a, b| | |
| a.class == b.class | |
| end | |
| def plus a, b | |
| p "plus by string" | |
| "#{a}#{b}" | |
| end | |
| concept :plus do |a, b| | |
| a.class == String && b.class == String | |
| end | |
| end | |
| p X.new.plus 1, 2 | |
| p X.new.plus "homu", "mami" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment