Pages

Thursday, July 7, 2011

Ruby Pair Programming

As we all know, Learning Ruby is fun, i just want to use this post to share my experience in Ruby pair programming. Initially i don't have any idea on pair programming and after i learnt i found that, this is one type of writing unit testing in Java. Also, its just like the approach of Test Driven Development, that is what i understood.

Writing tests and writing code based on that test and checking every time the test are passed whenever we do modifications... that is what finally pair programming is.. so what will be the advantage? New modifications in the code should not break the existing functionality as iteration is the important aspect in software development.

Our mentor Victor Goff teach this concept to me like how a father can teach to his kid... i really admired of his teaching skill and its my duty to say special thanks to him as this post is mainly because of his inspiration. I do not know whether i will post all the inputs that he shared with me, but i will try my best to achieve that.

Lets start jump towards the pair programming...

For that we need rubydoctest to be installed. My concentration is mainly towards Windows since for Mac and other OS there needs to me some more settings we need to do for installing that. Mostly these types are installed as gem and hence other OS users can do some google on it to identify the steps for rubydoc installation.

Just install the rubydoctest as below
You can choose the directory where Ruby is installed if it throws any error. I hope it will not throw any error but i am not sure... so try it if you want.

The next step is towards writing the program.
Create a file called hello_world.rb and type the following in it.

Now run the file in the command prompt as below and we will get the following result.The errors and failures are 0 and this is the good startup.
So, whatever doctest we need to write, we have to write it within the comment block = begin and =end

Next we are going to start by writing the first doctest for our program.
For that we just update the lines as below in the hello_world.rb file

when we run the rubydoctest, we will get the following outputSo, we are calling a method called hello_world but its not defined.
In order to correct this error, we can define the method as below.

Now when we again run the rubydoctest, we can see the ouput as belowNow the error becomes 0 but the failure is 1. That means the expected output doesn't match with the result output.
So, we have to think about how to correct it. The expected output is "Hello World!". We will update the code as like below and we will check whether we are able to correct this error or not.

Run the rubydoctest, and check the output which is as shown below.Now there are no errors and failures since the we get the expected output.

This is how the pair programming works and hence it mimimizes the error while coding.

Similar to the above steps, we can add 'n' number of doctest and we can check for the output result.

Whatever steps we learnt above is the basic for running the rubydoctest.
I can show two more iterations from which we can get more comfort with this process.

Iteration 1:

Add the following doctest and run the program.

doctest: I can pass a name to hello_world and get "Hello Mani!"
>> hello_world "Mani"
=> "Hello Mani!"
In order to correct the error, update the method as below:

Now when we run the doctest, we will get the output without errors as like below.So, by passing the arguments to the existing method, we are able to correct the error. Also, note that, the existing doctest is also not failed.

Iteration 2:

In this iteration, we just add one more argument to the method. We will write the doctest as like below.

doctest: I can also ask a question by passing "Erik", "?"
>> hello_world "Erik", "?"
=> "Hello Erik?"

By running the test, the ouput will be as like below.In order to correct the error, we need to modify the method as like below.

And the output after running the doctest will look like this.Now all the tests are passed and your code is working fine without affecting the existing functionality.

This is how the advantage of pair programming stands. We keep on adding the functionalities over by over. At the same time, some care needs to be there to check the existing thing and thats why we are checking all the tests. If any one of the test failed, that means we are doing something wrong and we will be cautious while coding.

If you understand the above concept, surely you will try to go to the next step in that. You can try this Blog post on Ruby Doctest of my mentor which will definitely help you to move little bit further. (I feel little bit hard :( while going through that.. May be because of getting the input directly from him)

Hope all enjoy this post and Happy pair programming until the next post.. :)