Created
May 12, 2021 19:35
-
-
Save jakenotjacob/d71ddbf2da4c94d6743d2d74d0abe428 to your computer and use it in GitHub Desktop.
Using Ruby module hooks to inject class methods
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 Executor | |
def self.included(classname) | |
puts "#{classname} included me, #{self}! I feel loved!" | |
puts "Now I am going to automagic some methods into #{classname}..." | |
classname.class_eval do | |
def scream | |
"OH MY GLOB" | |
end | |
end | |
end | |
end | |
class Thingie | |
include Executor | |
end | |
t = Thingie.new | |
t.scream |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment