-
-
Save iwadon/13426 to your computer and use it in GitHub Desktop.
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 M | |
| def subclasses | |
| @subclasses ||= [] | |
| end | |
| def inherited(subclass) | |
| subclasses << subclass | |
| end | |
| def const_missing(name) | |
| subclasses.each do |subclass| | |
| return subclass.const_get(name) if subclass.const_defined?(name) | |
| end | |
| super | |
| end | |
| end | |
| class A | |
| extend M | |
| def x | |
| C | |
| end | |
| end | |
| class B < A | |
| C = 100 | |
| end | |
| p B.new.x # => 100 | |
| p A.new.x # => 100 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment