Created
May 25, 2013 16:54
-
-
Save wheeyls/5649789 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
# inheritence question: | |
# When mixing in a module, how does super work? | |
# Does it behave the same for protected methods? | |
# And private methods? | |
class Parent | |
def log(name) | |
puts name | |
puts self.class | |
end | |
def self.inherited(base) | |
puts "Inherited: #{base}" | |
end | |
class << self | |
attr_accessor :static_value | |
def static_value | |
@static_value ||= self.class.to_s | |
end | |
end | |
def public_method; log('parent') end | |
protected | |
def protected_method; log('parent') end | |
private | |
def private_method; log('parent') end | |
end | |
module Cousin | |
def public_method | |
log 'cousin' | |
super | |
raise 'ouch' | |
rescue Exception =>e | |
puts "error rescued: #{e}" | |
end | |
protected | |
def protected_method | |
log 'cousin' | |
super | |
end | |
private | |
def private_method | |
log 'cousin' | |
super | |
end | |
end | |
class Child < Parent | |
prepend Cousin | |
def public_method | |
log 'child' | |
super | |
end | |
def wrap_protected | |
protected_method | |
end | |
def wrap_private | |
private_method | |
end | |
protected | |
def protected_method | |
log 'child' | |
super | |
end | |
private | |
def private_method | |
log 'child' | |
super | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment