drew.visited << “India”

August 19, 2007

Two slightly major pieces of news:

  1. I took a new job with Thoughworks as an application developer. This means I get to spend the majority of my time creating applications using ruby and java! And in an agile environment to boot! I’m extremely excited, my first day is this week.
  2. As a result of this career move, I’ll be going to India for two and a half weeks. I leave this week and I’m extremely excited. I’ll try to post a few pictures.

Expect the frequency of posts to increase in the near future as things have settled down on the job front. See you in the Garden City.


First, sorry for the lack of posting over the past month. Things have been fairly crazy/interesting on the job front. More on that later.

For now, I want to share a nifty little method I put together (I’m probably not the first one) to deal with a situation I recently encountered. I had a bunch of data in an array and I wanted to compare each element with the elements next to it, one at a time, and find any place where the difference between the numbers was more than 10. Here’s what I added to the Array class to accomplish this task:

Little-bitty code snippet

This allows me to perform the function I wanted simply by doing this:

my_array.adjacent_pairs.inject(0){|count,(i1,i2)| count + ((i1-i2).abs > 10 ? 1 : 0)}

Nice and easy. Ruby has made it a breeze for me the solve problems like this in simple, understandable ways without extra fluff.

It appears ruby is even smarter than I imagined; this functionality is already present!

require 'enumerable'
my_array.each_cons(2){|a,b| #do something with a and b }

More and more, it’s becoming apparent to me that we live in a world full of self reference and recursion, meta data and aggregators, collaboration and user generated content. Think about it for a second. Our world is full of recursive, self-referential examples. A movie trailer (a movie about the movie itself), blogs about blogging, meta-data (data about data), a YouTube video about YouTube. The examples go on and on.

I’ve recently begun thinking about these topics because of the book I am a Strange Loop. In it, Dogulas Hofstadter argues that consciousness arises as we become aware of the world, and therefore become aware of ourselves, and therefore become aware of ourselves being aware of ourselves being aware of the world, and therefore… on and on. This self reference cycle (referred to as a strange loop by Hofstadter) may, in fact, be the key to consciousness itself. Is the concept of “I” simply a recursive definition within the framework of our minds? I don’t know, but I do know that I really like this book.

On top of this, I recently stumbled across the YouTube video The Machine is Us/ing Us. In it, the author explains the elusive concept of Web2.0 by using Web2.0 tools. This bizarre and interesting approach seems to hit the nail on the head for me. Watch it a couple of times. Very cool.

I would say that the web is becoming more powerful now not simply because technology is advancing but because the way we think about thinking is changing. It seems to me that data itself is becoming less important than the ways in which the data is tracked, compiled, compared and communicated. To sum it all up, I leave you with this nifty phrase from Hofstadter’s book:

“preceded by itself in quotes forms a full sentence” preceded by itself in quotes forms a full sentence.

For more information, please see this article.

Update: A reader has added my post to his “lens” about strange-loops. I wasn’t familiar with lenses until today, but they seems to be a type of data aggregator, which I find very fitting. A web page containing information about web pages containing information about strange loops. What could be a more perfect self-referential showcase for this article?

Ruby has open classes. “Who cares?” you say. You should. Yes you! Open classes really simplify a lot of programming problems when you can learn to use them correctly. First and foremost, what does it mean to have open classes? Well, this means that you, the programmer, can crack open ANY class and add methods to your heart’s desire! And nothing is off limits. This means I can break into the Integer class, add a helpful method for my program, and sneak out again without anyone being the wiser.

So how would one use the the fiendishly clever practice of open classes in every day coding? Here’s an example of a problem I ran into while working on project euler problems:

I often found that I needed to know the number of digits in an Integer for solving these problems. Let’s say I want to get the sum of the number of digits in all the numbers 1..100. Here’s the old way:

Old Way

Now this is all fine and dandy and the code works great. However, there is a logical conundrum here. Why is num_digits just hanging out there by itself? We know that this method will always be called on Integers and, therefore, it makes much more sense to add a new instance method to the Integer class. Ruby open classes to the rescue!

Super fabulous open classes way!

Ahh, I feel so much better, don’t you? The only subtlety here is that we’re using self inside our num_digits method. This simply references the instance of the integer we are currently dealing with. So remember, next time you find yourself writing a method that would be better suited as an instance method of a class, break on in there! Ruby classes aren’t shy.

Programming as a hobby

April 27, 2007

I enjoy programming quite a bit, but seeing as I currently work in consulting, I don’t always get to program at work. However, it’s important to me to stay sharp with these skills, and this means programming as a hobby.

But how the heck do you program as a hobby? Unless you have a specific project you want to work on or a specific problem you’re trying to tackle, programming doesn’t have much of a use. As I see it, you’ve got 2 options: think of a project you want to work on or check out the sites I’m going to tell you about.

The sites I’m talking about are Codegolf and RubyQuiz. Both sites are great and both have contributed to my learning ruby over the past 8 or so months. Each site takes a slight different approach.

Codegolf is more of a competition. As the name references, the goal here is the solve a given programming problem with the absolute least amount of characters possible. This leads to crazy, hard to read, but interesting solutions. However, there’s a catch here: you can not view other’s solutions. You can discuss them on the forums, but as of right now all submissions are closed. On the up side, this does mean all challenges are always open. Forcing yourself to be concise leads to learning a lot about a language very quickly, as well. You can submit solutions in Php, Ruby, Python and Perl.

Rubyquiz, on the other hand, is what I would think of as a collaborative learning community. Each and every Friday a new “quiz” is posted to the website as well as the Ruby mailing list. Everyone has 48 hours to come up with a solution (this is a no spoiler period, don’t spoil it!) and then solutions are posted on the ruby mailing list. The following Thursday, the site’s author writes a summary of the solutions and discusses the best approaches. Best of all, the solutions are available for downloading. This site has exponentially improved my code.

Hope you enjoy these two sites, and happy coding!

I find the topics of Artificial Intelligence and Machine Learning extremely intriguing. In fact, one of the reasons I decided to study Computer Science was my perception of these concepts. I viewed them as extremely complex and equally difficult to implement.

Well, this isn’t always the case. In fact, I’ll show you an easy way to add some AI to your applications using an example. Let’s look at a snipet of code from my rubyquiz submission this week. Now, please, don’t think that I have delusions of grandeur and believe this to be an actual example of true AI, but think of this as barely scratching the surface to simply introduce how to think about adding some type of intelligent behavior to your application.

Anyway, this week’s quiz focused on getting a bit of Morse code from the user and then presenting all the possible translations of the code. I decided to add a nifty little feature that would make a “best guess” at which of the many possible translations was the true intent of the sender. How did I do it? Simply put: word frequency.

Sounds scary, but it’s not. My program read through a text document of a Sherlock Holmes novel, counted the number of times each word appeared and then stored these word -> frequency pairs in a hash. Then, when presenting my possible translations to the user, I simply showed the words with the highest occurrence in the Holmes novel first.

This clever (and simple, I don’t flatter myself) trick gives my program a bit of intelligence. In fact, this is a very simple example of a Bayesian filter (check wikipedia for more info). I’ll post the “intelligent” code snipet below. And just think; if I can do something like this, so can you. And probably something way cooler.

My code

From time to time, people talk about how dynamic languages aren’t suitable for “real” problems as they are quite a bit slower than static, compiled languages. However, I believe that the combination of dynamic languages’ expressiveness and speed more than make up for the lack of pure speed when compare to static languages. And guess what? Dynamic languages aren’t as slow as you think.

I ran across a perfect example today while reading the ruby mailing list. This post describes the implementation of a spelling correcter (much like the one wordpress is using now as a type this blog). Essentially, this application is given a word and then attempts to verify the correctness of it’s spelling. If the word is spelled incorrectly, it gives back suggestions for the word the user was trying to type. This type of technology is used by Google, among others. This problem seems to be quite daunting and, if one would attempt to solve it, it seems like the use of a static language for pure speed would be needed here. The truth is that this is not the case. Some folks who are much more clever than I am were able to write a 21 line Python script and a similarly sized Ruby script to accomplish this task. And they are fast (the Python script was able to process 10 lines per second, and the darn thing is not optimized at all).

Reading these solutions, I was struck with how completely expressive these languages truly are. Take a look at the post and I’m sure you’ll see what I mean. Reading solutions like this – programmed by obviously brilliant people in these two languages – lead me to believe that dynamic OO languages are the wave of the future.