Skip to content

Instantly share code, notes, and snippets.

@zdennis
Last active July 16, 2019 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zdennis/bc1dfbe623fdb3ac625508bc2dafb213 to your computer and use it in GitHub Desktop.
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?
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