Last active
July 16, 2019 16:11
-
-
Save zdennis/bc1dfbe623fdb3ac625508bc2dafb213 to your computer and use it in GitHub Desktop.
Given the below hierarchy how many ways can you think of to wrap the `Parent#foo` implementation so that `Grandchild1` and `Grandchild2` can have their own unique logic that executes after all of the mixin `foo`(s) but before the `Parent#foo` executes?
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 N | |
def foo | |
puts "N: foo" | |
super | |
end | |
end | |
module O | |
def foo | |
puts "O: foo" | |
super | |
end | |
end | |
class Parent | |
def foo | |
puts "foo" | |
end | |
end | |
class Child < Parent | |
include N | |
end | |
class Grandchild1 < Child | |
include O | |
end | |
class Grandchild2 < Child | |
include O | |
end | |
# Say we want this to wrap the this to output: | |
# O: foo | |
# N: foo | |
# Grandchild1 special foo | |
# foo | |
Grandchild1.new.foo | |
# And we wanted this to output: | |
# O: foo | |
# N: foo | |
# Grandchild2 special fu | |
# foo | |
Grandchild2.new.foo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment