Skip to content

Instantly share code, notes, and snippets.

@mdouchement
Created July 11, 2014 07:30
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 mdouchement/a5977b6c67e1b1587a53 to your computer and use it in GitHub Desktop.
Save mdouchement/a5977b6c67e1b1587a53 to your computer and use it in GitHub Desktop.
Ruby module (abstract) example
module Abstract
def self.included(base)
base.extend(ClassMethods)
end
def hello
'hello from instance method'
end
module ClassMethods
def hello
'hello from class method'
end
end
end
begin
puts Abstract.hello
rescue => e
warn e.message
end
puts '----------------------------------'
class Concrete
include Abstract
def say_toto
'I say toto'
end
end
puts Concrete.hello
concrete = Concrete.new
puts concrete.say_toto
puts concrete.hello
puts '----------------------------------'
class Concrete2
def say_titi
'I say titi'
end
end
concrete_2 = Concrete2.new
puts concrete_2.say_titi
begin
puts concrete_2.hello
rescue => e
warn e.message
end
concrete_2.extend(Abstract)
puts concrete_2.hello
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment