Paget, Bermuda

When I was studying in Greece, one of my classes was called Computational Intelligence, and in it we studied neural networks, genetic algorithms and fuzzy logic. It was actually a very interesting class, and we had projects in each of the aforementioned areas. For the genetic algos part we were told we could implement the genetic algorithm in whatever language we wanted. I chose Ruby, always being a tad unorthodox. It was my first serious attempt to use Ruby, although I had been dabbling for some time. It was quite pleasant to write, and while Ruby isn’t really appropriate for the serious number crunching of genetic algos it was a nice exercise.

However, I was just setting up my development environment on my laptop (more on the laptop in a separate post forthcoming) and decided to see what my Ruby setup was like. Leopard comes with 1.8.6 which is pretty good. It doesn’t have the mysql bundle installed, so I tried to get it via gem. It downloaded, but didn’t build. I’m not familiar with the gem system, perhaps there is a way to provide configuration switches through it, but I went into the download directory and was able to massage it into working. Not really Ruby’s fault I guess, but that is beside the point, I was able to get it installed.

I popped into irb, which is the interactive ruby shell, and tried to see if it “took”.

>> require 'mysql' 
=> true

OK, that is a good start. That means the extension was loaded successfully. Lets check how easy it is to access.

>> db = Mysql.connect('localhost', 'username', 'password', 'movies', 3306, '/var/mysql/mysql.sock')
=> #<Mysql:0x577448>
>> res = db.query("SELECT movie_id, title FROM movies WHERE available='Y'")
=> #Mysql::Result:0x56cad4>

Two commands and I had a result set filled with my available movies. Could it really be this easy? How about printing it out?

>> res.each() {|x| puts "[#{x[0]}] #{x[1]} " }
[2] Mallrats 
[3] Lock, Stock & Two Smoking Barrels 
[7] Daredevil 

And BAM! Just like that. I guess the adage that Ruby is a programmers best friend is pretty close to the mark.