Skip to content

Instantly share code, notes, and snippets.

@chadfennell
Created November 30, 2018 19:09
Show Gist options
  • Save chadfennell/11b7be5ffd69ada144c2d4126b6a9143 to your computer and use it in GitHub Desktop.
Save chadfennell/11b7be5ffd69ada144c2d4126b6a9143 to your computer and use it in GitHub Desktop.
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