Skip to content

Instantly share code, notes, and snippets.

@gabrieljoelc
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabrieljoelc/5289596099c0ed40e2bc to your computer and use it in GitHub Desktop.
Save gabrieljoelc/5289596099c0ed40e2bc to your computer and use it in GitHub Desktop.
Ruby inheritance, inner classes, and class methods with `const_get`
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
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
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
@gabrieljoelc
Copy link
Author

I wonder if I can use this: http://stackoverflow.com/a/18684467

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment