Tag: ruby

Haml, Sinatra and HTML attributes

I’m in the midst of creating a new web project which is small enough that it has a good chance of getting done. I using it as an excuse/reason to dabble with something new, technology-wise. To balance all the Microsoft that I get at work, I’m playing with Ruby again. Only this time not with Rails. Rails is so much more than I need for this. This time I am playing with HAML, Sinatra and at some point Passenger. If these things don’t interest you, then you have my permission to stop reading now, because it isn’t going to get better.


read more →

October 28, 2009

Ruby-licious

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.

January 11, 2008

Weather

Bermuda: 15°C
Halifax: 0.3°C

Backpack: Get Organized and Collaborate
You cannot be anything you want to be — but you can be a lot more of who you already are. — Tom Rath