Created
June 1, 2015 13:36
-
-
Save adambray/e918829883c497776420 to your computer and use it in GitHub Desktop.
Metaprogramming - Calculator Interface
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
# Calc class | |
# my_calc = Calc.new | |
# my_calc.one.plus.one => 2 | |
# my_calc.two.plus.one => 3 | |
# my_calc.four.minus.one => 3 | |
#my_calc.one => new_calc_object | |
#new_calc_object.plus => newer_calc_object | |
#newer_calc_object.one => 2 | |
class Calc | |
def initialize | |
@operation = nil | |
@first_number = nil | |
end | |
def handle_operation(value) | |
if @operation | |
return @first_number.send(@operation, value) | |
else | |
@first_number = value | |
return self | |
end | |
end | |
def one | |
handle_operation(1) | |
end | |
def two | |
handle_operation(2) | |
end | |
def plus | |
@operation = "+" | |
return self | |
end | |
def minus | |
@operation = "-" | |
return self | |
end | |
# def method_missing(name, args) | |
# puts "I don't know how to #{name} with #{args}" | |
# end | |
end | |
# Fridge.limit(4).where(name: "blah").where(location: "boop").order(name: :desc) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment