Skip to content

Instantly share code, notes, and snippets.

@joejag
Forked from mowat27/calculator.rb
Last active December 21, 2015 15:08
Show Gist options
  • Save joejag/6324167 to your computer and use it in GitHub Desktop.
Save joejag/6324167 to your computer and use it in GitHub Desktop.
class Calculator
def calculate(expresion)
return 0 if expresion.split.size != 3
parts = expresion.split
parts[0].to_i.send(parts[1], parts[2].to_i)
end
end
describe Calculator do
let(:calculator) { Calculator.new}
it "adds numbers" do
calculator.calculate("1 + 2").should == 3
end
it "adds big numbers" do
calculator.calculate("11 + 22").should == 33
end
it "subtracts numbers" do
calculator.calculate("2 - 1").should == 1
end
it "multiplies numbers" do
calculator.calculate("2 * 3").should == 6
end
it "divides numbers" do
calculator.calculate("12 / 3").should == 4
end
it "evaluates an empty string as zero" do
calculator.calculate("").should == 0
end
it "evaluates nonsense to zero" do
calculator.calculate("+ 1 2 3").should == 0
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment