You may not have realized it, but learning Ruby is like being attacked by a bunchof ADD Buddhist Monks. They’ll jump into your living room, raving and excited, anddo back flips while telling you how Zen and relaxing development should be. It’s weird.
Once we’ve medicated the frenetic relaxation, though, it’s really evident that therea lot of good ideas to be found in this community. The latest that I’ve discoveredis Test-Driven Learning.
What this means to me is that I can FINALLY stop writing ‘Hello World’ programs. Languageshave print or printf or puts commands—we can stop being thrilled by that now. Ok?Let’s just write Tests! If we can make test Assertions, we can kick the tires of eachnew piece of language syntax we learn as we learn it without creating silly, baroqueprograms to host them.
Lets start out by discovering the shortest syntax to use the language’s testing framework.The one built into Ruby is Test::Unit. This is easy in Ruby. You need a ‘require’line and to subclass the test fixture class.
require "test/unit"
class TestTest< Test::Unit::TestCase
def test_tests
assert true
end
end
That’s easy. And now you can assert things. Like ‘true’. Save that in a file, like‘test.rb’ then execute it with the ruby command line.
C:UsersjoshDesktop> ruby test.rb
Loaded suite test_test
Started
.
Finished in 0.001seconds.
??
1 tests, 1 assertions, 0 failures, 0 errors
From here on out, just start adding ‘assert’ lines as you discover things about Ruby.Since we started with ‘true’, maybe some asserts on Truth would be a good start. Iwonder which of these will pass and which will fail? If you know Perl, Javascript,or C# your preconceptions will be different.
assert true
assert false
assert 'True'
assert ""
assert 0
assert 1
So little code, and now you can curl up with your favorite Ruby howto book and spenda warm afternoon pondering the nature of Truth. Exciting eh? Well calm down! You don’tneed to be a crazy Zen guy. Just do some learning.
Resources:
- “Ruby Learning Test #1:Are You There, World?” by Mike Clark
- Why’s (Poignant) Guide to Ruby
- Test::Unit atruby-doc