Skip to content

Instantly share code, notes, and snippets.

@mmower
Created September 21, 2009 22:02
Show Gist options
  • Save mmower/190596 to your computer and use it in GitHub Desktop.
Save mmower/190596 to your computer and use it in GitHub Desktop.
class Calculator
def equation(val1, val2)
result = val1 + val2
return result
end
end
# Okay so the only real point here is one of style. In Ruby it's not necessary
# to use 'return'. By default a method returns the value of the last expression
# evaluated. So your method can be rewritten
def equation( val1, val2 )
val1 + val2
end
require 'calculator'
describe Calculator do
it "should perform calculations" do
calc = Calculator.new
result = calc.equation(1, 5)
result.should == 6
end
end
# Again it's a matter of style and each case is different by I would write this
calc.equation(1,5).should == 6
# Assignment to temporary variables is sometimes helpful but, generally, I do it
# after the fact in order to simplify an expression group that has gotten
# complex, or to cache a value that is *known* to be expensive to compute. You
# could even take it a step further and write
Calculator.new.equation(1,5).should == 6
# but I think that's a little bit ugly. On the other hand the creation of the
# calculator test asset might be better done elsewhere. Tough to say.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment