Created
April 22, 2014 17:12
-
-
Save mathieugagne/11187161 to your computer and use it in GitHub Desktop.
Fluent Calculator
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
class Calc | |
DIGITS = [:zero, :one, :two, :three, :four, :five, :six, :seven, :eight, :nine] | |
def method_missing method | |
if DIGITS.include?(method) | |
DIGITS.index(method) | |
else | |
super | |
end | |
end | |
end | |
describe Calc do | |
@calc = Calc.new | |
describe 'digits' do | |
it 'returns its numeric value' do | |
Test.assert_equals(@calc.one, 1) | |
Test.assert_equals(@calc.four, 4) | |
Test.assert_equals(@calc.nine, 9) | |
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
class Calc | |
DIGITS = [:zero, :one, :two, :three, :four, :five, :six, :seven, :eight, :nine] | |
DIGITS.each do |digit| | |
define_method(digit) do | |
DIGITS.index(digit) | |
end | |
end | |
end | |
describe Calc do | |
@calc = Calc.new | |
describe 'digits' do | |
it 'returns its numeric value' do | |
Test.assert_equals(@calc.one, 1) | |
Test.assert_equals(@calc.four, 4) | |
Test.assert_equals(@calc.nine, 9) | |
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
class Calc | |
DIGITS = [:zero, :one, :two, :three, :four, :five, :six, :seven, :eight, :nine] | |
attr_accessor :operand, :operator | |
DIGITS.each do |digit| | |
define_method(digit) do | |
numeric_value = DIGITS.index(digit) | |
if @operand and @operator | |
@operand.send(@operator, numeric_value) | |
else | |
@operand = numeric_value | |
self | |
end | |
end | |
end | |
end | |
describe Calc do | |
describe 'digits' do | |
it 'returns itself' do | |
Test.assert_equals(Calc.new.one.class, Calc) | |
end | |
it 'returns its numeric value' do | |
Test.assert_equals(Calc.new.one.operand, 1) | |
Test.assert_equals(Calc.new.four.operand, 4) | |
Test.assert_equals(Calc.new.nine.operand, 9) | |
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
class Calc | |
DIGITS = [:zero, :one, :two, :three, :four, :five, :six, :seven, :eight, :nine] | |
DIGITS.each do |digit| | |
define_method(digit) do | |
numeric_value = DIGITS.index(digit) | |
if @operand | |
@operand.send(@operator, numeric_value) | |
else | |
@operand = numeric_value | |
self | |
end | |
end | |
end | |
attr_accessor :operand, :operator | |
def plus | |
@operator = :+ | |
self | |
end | |
def minus | |
@operator = :- | |
self | |
end | |
def times | |
@operator = :* | |
self | |
end | |
def divided_by | |
@operator = :/ | |
self | |
end | |
end | |
describe Calc do | |
describe 'digits' do | |
it 'returns itself' do | |
Test.assert_equals(Calc.new.one.class, Calc) | |
end | |
it 'returns its numeric value' do | |
Test.assert_equals(Calc.new.one.operand, 1) | |
Test.assert_equals(Calc.new.four.operand, 4) | |
Test.assert_equals(Calc.new.nine.operand, 9) | |
end | |
end | |
describe 'operators' do | |
it 'returns itself' do | |
Test.assert_equals(Calc.new.one.plus.class, Calc) | |
end | |
it 'sets the operator' do | |
Test.assert_equals(Calc.new.one.plus.operator, :+) | |
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
class Calc | |
DIGITS = [:zero, :one, :two, :three, :four, :five, :six, :seven, :eight, :nine] | |
DIGITS.each do |digit| | |
define_method(digit) do | |
numeric_value = DIGITS.index(digit) | |
if @operand | |
@operand.send(@operator, numeric_value) | |
else | |
@operand = numeric_value | |
self | |
end | |
end | |
end | |
attr_accessor :operand, :operator | |
def plus | |
@operator = :+ | |
self | |
end | |
def minus | |
@operator = :- | |
self | |
end | |
def times | |
@operator = :* | |
self | |
end | |
def divided_by | |
@operator = :/ | |
self | |
end | |
end | |
describe Calc do | |
describe 'digits' do | |
it 'returns itself' do | |
Test.assert_equals(Calc.new.one.class, Calc) | |
end | |
it 'returns its numeric value' do | |
Test.assert_equals(Calc.new.one.operand, 1) | |
Test.assert_equals(Calc.new.four.operand, 4) | |
Test.assert_equals(Calc.new.nine.operand, 9) | |
end | |
end | |
describe 'operators' do | |
it 'returns itself' do | |
Test.assert_equals(Calc.new.one.plus.class, Calc) | |
end | |
it 'sets the operator' do | |
Test.assert_equals(Calc.new.one.plus.operator, :+) | |
end | |
end | |
describe 'operations' do | |
it 'performs additions' do | |
Test.assert_equals(Calc.new.one.plus.three, 4) | |
end | |
it 'performs substraction' do | |
Test.assert_equals(Calc.new.nine.minus.four, 5) | |
end | |
it 'performs multiplication' do | |
Test.assert_equals(Calc.new.two.times.six, 12) | |
end | |
it 'performs divisions' do | |
Test.assert_equals(Calc.new.nine.divided_by.three, 3) | |
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
class Calc | |
DIGITS = [:zero, :one, :two, :three, :four, :five, :six, :seven, :eight, :nine] | |
OPERATORS = {plus: :+, minus: :-, times: :*, divided_by: :/} | |
attr_accessor :operand, :operator | |
DIGITS.each do |method_name| | |
define_method(method_name) do | |
numeric_value = DIGITS.index(method_name) | |
if @operand | |
@operand.send(@operator, numeric_value) | |
else | |
@operand = numeric_value | |
self | |
end | |
end | |
end | |
OPERATORS.each do |method_name, operator| | |
define_method(method_name) do | |
@operator = operator | |
self | |
end | |
end | |
end | |
describe Calc do | |
describe 'digits' do | |
it 'returns itself' do | |
Test.assert_equals(Calc.new.one.class, Calc) | |
end | |
it 'returns its numeric value' do | |
Test.assert_equals(Calc.new.one.operand, 1) | |
Test.assert_equals(Calc.new.four.operand, 4) | |
Test.assert_equals(Calc.new.nine.operand, 9) | |
end | |
end | |
describe 'operators' do | |
it 'returns itself' do | |
Test.assert_equals(Calc.new.one.plus.class, Calc) | |
end | |
it 'sets the operator' do | |
Test.assert_equals(Calc.new.one.plus.operator, :+) | |
end | |
end | |
describe 'operations' do | |
it 'performs additions' do | |
Test.assert_equals(Calc.new.one.plus.three, 4) | |
end | |
it 'performs substraction' do | |
Test.assert_equals(Calc.new.nine.minus.four, 5) | |
end | |
it 'performs multiplication' do | |
Test.assert_equals(Calc.new.two.times.six, 12) | |
end | |
it 'performs divisions' do | |
Test.assert_equals(Calc.new.nine.divided_by.three, 3) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment