Archive for January, 2006

Solving mathematical problems with Ruby

Antonio Cangiano January 30th, 2006

A couple of weeks ago I’ve discovered a very interesting and addictive website MathsChallenge.net . They have a section called Project Euler where participants can try to resolve more than 100 mathematical problems. The scoring system is really well done and the discussion forum for each problem is accessible only after having solved the given problem.

It’s interesting to see how other people try to figure out a solution and what languages they employ to solve the puzzling problems. I even discovered rare languages like J or K through this site. It’s like a martial arts contest where participants can use their favorite martial art, only this is for hackers. :-)

I’ve only spent a few hours over a couple of days on the site, enough time to solve 26 problems and bring me somewhere around 370th place out of more than 1300 active participants in the contest. I didn’t spend time solving problems during the last ten days, and in fact I am now 418th. I am going to work on more problems when I’ll have some time. I love this kind of stuff, and it’s not just for the enjoyable competition. It doesn’t really matter if you are first or last, especially if you know how to solve most of the problems but you are short of time like me. What’s important is to have fun and benefit from the learning process.

The programs must solve the given problems within 60 seconds (think of it like Gone in 60 Seconds), which means that most brute force methods won’t work.

This is particularly true for Ruby. In fact I have seen some people with C or Assembler resolve a problem with brute force algorithms, where Ruby would take way longer than a minute. There is no harm in this though, it’s a good exercise in controlling the algorithms’ complexity, facing problems that you don’t encounter too often in real world programming.

I use Ruby 1.8.4 so I even had to rewrite my own mathn library because prime computation and gcd are exceptionally slow (1.9 solves the problem though).

I invite you to join me and bring up Ruby statistics on MathsChallenge. And above all, have lots of fun…

Ruby Cookbook: Rough Cuts Version

Antonio Cangiano January 30th, 2006

Ruby  Cookbook

Currently online O’Reilly is selling the rough cuts version (basically the beta version) of the Ruby Cookbook. It’s a pleasure to see that many good books about Ruby are being published.

This book authored by Leonard Richardson and Lucas Carlson is particularly special to me. In fact, I have contributed to the book with three recipes and related full-length explanations. My recipes are about using RMagick to achieve common image manipulation tasks, as I love photography and I like RMagick a lot!

The recipes are Thumbnailing Images, Adding Text to an Image and Converting Among Image Formats. While these tasks are quite easy to achieve thanks to Tim Hunter’s library, I tried to go the extra mile and provide a good sort of introduction to the library, within the limits of the problem/solution framework of these kind of books.

It’s my first contribution to an O’Reilly book and I’m humbly proud of it. I look at it as a stepping stone until the day when I’ll publish my own book about Ruby :-)

Ruby rocks and Java sucks?

Antonio Cangiano January 22nd, 2006

Java VS Ruby

Many hackers are quite religious about their programming language of choice, but I’m not. Whenever possible I try to be rational about things that matter to me. For this reason you won’t hear me saying things like: “Ruby rocks, Java sucks!”. Indeed Ruby rocks, but it does so for me, for the kind of development that I love to do and for the way I like to think and express my thoughts in code.

I am quite confident that Ruby would be a great programming language for a broad audience of hackers worldwide, but this doesn’t mean that we should belittle those who happen to work with Java, C# or other “bureaucratic” language. On the web there are thousands of threads about “Java vs Ruby”, “PHP vs Rails”, “Python vs Ruby”, “C# vs Java”, and so on… this is mostly non-sense. Of course all these languages have differences that will determine your life as programmer, but you really need to go and try them out for yourself. Spend time selecting and learning your development platform, experiment, and if possible learn more than one language and framework. Different paradigms can be a huge gain for the programmer in terms of flexibility. I really love to work with drills, but sometimes you need a screwdriver, or just a hammer. “Buy some tools” keep them in your toolbox, and then use the ones that will let you do the job better. Or that are requested by the company were you work or by the client, or again that you enjoy using the most.

Some programming languages let you be more productive than others in particular circumstances, create a more maintainable code and so forth. Assembler is definetely less “human-friendly” than Python for example. But in the field of general purposes languages, the real bottleneck is often the programmer not the language! It’s your level of mastery of the language, that makes the difference in most cases. I love Ruby and it is in my opinion, a slightly better language than Python, but at the moment I am not half as productive in Ruby as Alex Martelli is in Python. :-)

One day my wife asked me why I was so passionate about Ruby, while I used to be interested in C# a lot. She asked me what the difference is and the advantages are? Now, she is a very smart woman, but she hasn’t a programming background so I tried to explain to her without going into details like dynamic languages, meta programming, ORM, and so on. She mostly got what I meant but I was looking for a more effective and direct way of illustrating the concept.

A few days ago I remembered the funny photo that you can see on the top righthand side1. That’s probably the best answer: simplicity. Ruby doesn’t get in the way and it lets you express your ideas, because Ruby is as simple as possible but not simpler as Einstein would say. You don’t need a dozen books to master Ruby or Web development with Rails, you just need a couple of books, a brain and some practice.

(1) I’d like to give credit to the author of the photo, but I don’t know who originally took the picture. I’ve only added the Einstein quote and rounded the corners.

Next »