Created
November 25, 2013 13:23
-
-
Save sbellware/7641110 to your computer and use it in GitHub Desktop.
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 Calculate | |
attr_accessor :num1 | |
attr_accessor :num2 | |
def calculate | |
self.! | |
end | |
end | |
class Arithmetic < Module | |
def self.[](operation_module) | |
new operation_module | |
end | |
def initialize(operation_module) | |
mod = operation_module | |
define_method :operation_module do | |
return mod | |
end | |
include operation_module | |
include Calculate | |
end | |
end | |
module Add | |
def ! | |
num1 + num2 | |
end | |
end | |
module Multiply | |
def ! | |
num1 * num2 | |
end | |
end | |
class Calculation | |
include Arithmetic[Add] | |
end | |
calculation = Calculation.new | |
calculation.num1 = 2 | |
calculation.num2 = 5 | |
puts calculation.calculate | |
# => 7 | |
class Calculation | |
include Arithmetic[Multiply] | |
end | |
calculation = Calculation.new | |
calculation.num1 = 2 | |
calculation.num2 = 5 | |
puts calculation.calculate | |
# => 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment