From: Ryan Davis Date: 2012-06-01T10:26:54+09:00 Subject: Re: need help with this On May 31, 2012, at 17:50 , Spencer Pottebaum wrote: > This is a homework question and I'm really confused by it: > > the method exp(x,y) is passed two integers and returns x^Y write a > recursive version of exp(s,y) in Ruby. > > this is what I have: > > def exp(x,y) > > if y = 1 > return x > > return x * exp(x, y-1) > > is this correct? Instead of answering you, I'm going to ask you how you'd answer it yourself. Given the function: def assert_equal expected_value, actual_value you can write the following form: assert_equal z, exp(x, y) What triplets of [x, y, z] would you need to verify that your function is working correctly? Once you figure out what tests you need, knowing if your code is working is pretty much automatic. Put this in a file "test_exp.rb" and try it out: require "exp" require "minitest/autorun" class TestHomework < MiniTest::Unit::TestCase def test_exp # ... fill in the blanks ... end def test_exp_negative_cases # ... fill in the blanks ... end # ... etc ... end Once all the tests are running, you might want to ask yourself how many different ways your function can be implemented. You can then modify as you see fit and you'll always have your tests to verify that your modifications are still working.