Last active
June 3, 2020 19:21
-
-
Save jdwolk/4e33204b232168bc9b64bc1fb769e0ce to your computer and use it in GitHub Desktop.
Ruby superclass/subclass AOP
This file contains 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 LogOnCall | |
def call | |
puts "Logging #{self.class.name}.call" | |
super | |
end | |
end | |
class BaseThing | |
### MAGIC HAPPENS HERE | |
def self.inherited(subclass) | |
subclass.prepend LogOnCall | |
end | |
### | |
end | |
class Thing < BaseThing | |
def call | |
puts "Heyo!!!" | |
end | |
end | |
# pry(main)> Thing.new.call | |
# Logging Thing.call | |
# Heyo!!! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment