Created
July 11, 2014 07:30
-
-
Save mdouchement/a5977b6c67e1b1587a53 to your computer and use it in GitHub Desktop.
Ruby module (abstract) example
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 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