This blog no longer exists.
You will be redirected to something cool.

Thursday, October 27, 2011

Ruby DocTest with Victor Goff of RubyLearning

I did a Google+ hangout/interview with +Victor Goff who taught me how to use Ruby DocTest. It reminded me of RSpec, but the key difference is that Ruby DocTest is used with IRB returns. Nifty, right?

Of course, I had to install a new gem. You can install it by running:
gem install rubydoctest

Victor created a file, hello.rb which contains code in a comment block. By running the following command, we ran an IRB session in the code comments:
rubydoctest hello.rb
This is what we created:

The => line is the 'expectation, or the assertion, or the 'should be' line as it would be for RSpec.

I learned that upon the creation of a new test that causes other tests to fail, it's best to ignore the older tests and work on the new feature until it passes.

On a side note, unrelated to Ruby DocTest, I also learned how to use a default param in Ruby. I had already known what it was, but this was the first time I got to see it in action. :P

Why would I choose Ruby DocTest over RSpec?
When teaching new programmers, and new programmers to Ruby, they are going to use IRB to do their testing anyway. Why have the RSpec/minitest/test-unit framework overhead to learning? You can IMMEDIATELY get them thinking about how their methods return things, and testing, and measuring... with 0 additional overhead in learning. Also, it shows them that documentation and examples in their own programs is easy to do, and it doesn't get 'stale' when it is executable. They learn that they can safely refactor under test.

The advantage to having documentation would be that you can use rdoc to document your methods (and it is testable) and it also serves as example usage for those using your documentation.

In Real Life... Ruby DocTest for what I use it for is small snippets and portions of programs that I may not understand the domain well enough to actually write tests first. The IRB session right there let's me get feedback from my program... (And I don't necessarily have to open up an IRB session).

Victor Goff writes for where programmers can take courses to further their Ruby (and general programming) education. I met Victor on Google+ and while I've not participated in his RubyLearning courses, they appear to be very interesting and educational. (Definitely worth checking out.)


And I thought we had fun programming a simple method with testing.

I hope to be able to pair with you again soon! It has already been a while.

It definitely has! I would love to learn more! I may take a course over the summer after school lets out. That'd be awesome!