Skip to content

Instantly share code, notes, and snippets.

@nunosilva800
Created May 22, 2018 11:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nunosilva800/4ba376eecc838fee38858f7eac9ea668 to your computer and use it in GitHub Desktop.
Save nunosilva800/4ba376eecc838fee38858f7eac9ea668 to your computer and use it in GitHub Desktop.
ruby interfaces example
module APIProfile
def profile
raise NotImplementedError.new("You must implement '#{__method__}'.")
end
end
module APIAddress
def address
raise NotImplementedError.new("You must implement '#{__method__}'.")
end
end
class APIProviderA
include APIProfile
end
class APIProviderB
include APIProfile
include APIAddress
end
# ---
> APIProviderA.new.respond_to? :profile
=> true
> APIProviderA.new.profile
NotImplementedError: You must implement 'profile'.
> APIProviderA.new.respond_to? :address
=> false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment