I wrote my first Ruby program today - a solution for the Ruby Quiz Look and Say Problem. I cheated a little by writing it first in Java and then converting the code to Ruby, but I’m not going to be a Ruby expert in an afternoon. Hell, I won’t even be a Ruby beginner in an afternoon, but I will have a Ruby program with unit tests and associative arrays. I’m happy right now though writing Ruby with Java syntax and that’s enough.
My solution is test-first (as usual). Here is the Java Test and the main class and here is the Ruby test and the main class.
There’s so much to know in the technology field that it’s easy to get overwhelmed. The basics such as memory and threading go deep and new libraries for these applications are being developed all the time. I feel I get lost sometimes fighting fires and office politics. In my ideal job, I would have time to follow the latest developments in GUIs and would translate that knowledge to creating better interfaces for my users.
So I guess it’s a sweet indulgence when I write a computer program in a new language to solve a problem like figuring out the recurrence pattern for the “Look and Say” problem. And the solution is:
$ ruby count_and_say_test.rb
Loaded suite count_and_say_test
0 LOOK AND SAY
1 TWO A ONE D ONE K ONE L ONE N TWO O ONE S ONE Y
2 ONE A ONE D SIX E ONE K ONE L SEVEN N NINE O ONE S TWO T TWO W ONE Y
3 ONE A ONE D TEN E TWO I ONE K ONE L TEN N NINE O THREE S THREE T ONE V T HREE W ONE X ONE Y
...
605 ONE A ONE D TWENTY-FIVE E FIVE F ONE G SIX H FOUR I ONE K THREE L NINE N
NINE O EIGHT R TWO S ELEVEN T THREE U FIVE V FOUR W TWO X TWO Y
606 ONE A ONE D NINETEEN E SIX F TWO G FOUR H EIGHT I ONE K TWO L ELEVEN N T
EN O FIVE R TWO S NINE T THREE U FIVE V FIVE W TWO X TWO Y
607 ONE A ONE D EIGHTEEN E FIVE F TWO G THREE H EIGHT I ONE K TWO L ELEVEN N
TEN O THREE R TWO S TEN T TWO U FIVE V SIX W TWO X ONE Y
Found loop to 178 after 607 iterations
.
Finished in 2.047 seconds.1 tests, 0 assertions, 0 failures, 0 errors
On a related note, Weiqi Gao touched on the Sapir-Whorf_hypothesis with regards to Java programmers like me just not getting Ruby, because it isn’t our native tongue.
Comment by tito — September 12, 2007 @ 10:10 pm