Skip to content

Instantly share code, notes, and snippets.

@wheeyls
Created May 25, 2013 16:54
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 wheeyls/5649789 to your computer and use it in GitHub Desktop.
Save wheeyls/5649789 to your computer and use it in GitHub Desktop.
# 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