Skip to content

Instantly share code, notes, and snippets.

@joallard
Last active December 16, 2015 05:38
Show Gist options
  • Save joallard/5385862 to your computer and use it in GitHub Desktop.
Save joallard/5385862 to your computer and use it in GitHub Desktop.
Ruby Modules and Duck-typing
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