Created
November 30, 2018 19:09
-
-
Save chadfennell/11b7be5ffd69ada144c2d4126b6a9143 to your computer and use it in GitHub Desktop.
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 MyForwardable | |
def def_delegator(obj, method) | |
define_method(method) do | |
self.public_send(obj).public_send(method) | |
end | |
end | |
end | |
class Dog | |
def speak | |
puts "Bark! Bark! Bark! I am a good boy!" | |
end | |
end | |
class SayHello | |
extend MyForwardable | |
def_delegator :pet, :speak | |
attr_reader :pet | |
def initialize(pet: Dog.new) | |
@pet = pet | |
end | |
end | |
SayHello.new.speak |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment