Created
October 15, 2013 22:16
-
-
Save raul-gracia/6999535 to your computer and use it in GitHub Desktop.
A tiny example of how to use ruby idioms
This file contains hidden or 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 ModuleA | |
def self.included(base) | |
base.send :extend, ClassMethods | |
base.send :include, InstanceMethods | |
end | |
module ClassMethods | |
def my_method | |
puts 'ModuleA#my_method' | |
end | |
end | |
module InstanceMethods | |
def batman | |
puts 'ModuleA#new#batman' | |
end | |
end | |
end |
This file contains hidden or 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 ModuleB | |
def self.included(base) | |
base.send :extend, ClassMethods | |
base.send :include, InstanceMethods | |
end | |
module ClassMethods | |
def other_method | |
puts 'ModuleB#other_method' | |
end | |
end | |
module InstanceMethods | |
def superman | |
puts 'ModuleB#new#superman' | |
end | |
end | |
end |
This file contains hidden or 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
require_relative 'moduleA.rb' | |
require_relative 'moduleB.rb' | |
class MyClass | |
include ModuleA | |
include ModuleB | |
end | |
MyClass.my_method | |
MyClass.other_method | |
MyClass.new.superman | |
MyClass.new.batman |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment