Last active
December 16, 2015 05:38
-
-
Save joallard/5385862 to your computer and use it in GitHub Desktop.
Ruby Modules and Duck-typing
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 Drinker | |
# can do, same implementation thank duck typing | |
def resolve_thirst | |
drink_water | |
end | |
end | |
class Person | |
include Drinker | |
# can do, different implementation | |
def drink_water | |
take_glass | |
pour_glass_into_mouth | |
swallow | |
end | |
end | |
class Dog | |
include Drinker | |
# can do, different implementation | |
def drink_water | |
water_source = find_water_source | |
shove_face_into(water_source) | |
end | |
def find_water_source | |
return lake if lake = Lake.nearby | |
return toilet if location.in_home? | |
raise WaterNotFoundError | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment