Last active
August 29, 2015 14:05
-
-
Save gabrieljoelc/5289596099c0ed40e2bc to your computer and use it in GitHub Desktop.
Ruby inheritance, inner classes, and class methods with `const_get`
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 Ruby2Prepend | |
module GetExtensions | |
module ClassMethods | |
def get | |
super + 1 | |
end | |
end | |
def self.prepended(base) | |
class << base | |
prepend ClassMethods | |
end | |
end | |
end | |
class BaseClient | |
prepend GetExtensions | |
def self.get | |
puts self.const_get 'ResponseData' | |
1 | |
end | |
end | |
class DemoClient < BaseClient | |
def ouch | |
self.class.get | |
end | |
class ResponseData | |
end | |
end | |
end | |
Ruby2Prepend::DemoClient.new.ouch # => 2 |
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 Ruby2Prepend | |
module GetExtensions | |
def insta_get | |
super + 1 | |
end | |
end | |
class BaseClient | |
prepend GetExtensions | |
def insta_get | |
1 | |
end | |
end | |
class DemoClient < BaseClient | |
def insta_ouch | |
insta_get | |
end | |
end | |
end | |
Ruby2Prepend::DemoClient.new.insta_ouch # => 2 |
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 RubySubTyping | |
class BaseClient | |
def self.get | |
self.const_get 'ResponseData' | |
end | |
end | |
class DemoClient < BaseClient | |
class ResponseData | |
end | |
end | |
end | |
Zen::DemoClient.get # => RubySubTyping::DemoClient::ResponseData |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I wonder if I can use this: http://stackoverflow.com/a/18684467