This is going to be brief one:, one of the projects I’m working on during my spare time, was giving me major headaches lately – 2 weeks ago, it took like 15 seconds to render a page. In the last couple of days, it was taking somewhere around 100 seconds to render it; obviously too much for the nginx server that was running my two mongrels.

I *had* indexed my tables, by id, url and several other fields. Turns out, they weren’t indexed with the right columns.


Other blogs about Ruby or Rails have already touched some of the tips I’m going to show, but it never hurts to remind you these small recipes aimed at Ruby novices:

  1. Default method parameters as a hash:
    I tend to use this when the argument list tends to vary, and I want to have a couple of nice defaults for the method attributes. This is actually what Rails uses intensively.


I keep having the following problem when dealing with svn: I frequently find myself wanting to add to my own SVN repository stuff(javascript libraries, rails plugins) retrieved from some other svn repositories. The inevitable outcome of my bold attempt is that SVN starts complaining that the new folders can’t be submitted, that they already are under code control or something.

The reason is because when copying entire FOLDERS from one svn repository to another, you also copy their associated .svn folders with svn-specific information. These .svn folders are usually hidden, but you can see them (on Unix/Linux/OSX) by running ls -la from the terminal.

So you see my problem: I want to copy files or folders into my code repository, but don’t want to involve svn into this. I want them to be added as new files/folders, regardless of where they came from.

The following command-line command has helped me avoid quite a bit of frustration today:

find . -name "*.svn" -print
find . -name "*.svn" -ok rm -rf {} \;

The first command lists recursively all svn-specific subfolders of the current one.
The second command displays them to me one after the other and patiently awaits I press ‘y’ to remove it.
Extremely useful, right?
For many more uses of the find command, check out this page – where I learned about the useful ‘ok rm’ trick.

Here’s the quick story: I was hoping to win the Romanian lottery (6 winning numbers out of 49). On the website, they were having the list of the winning numbers from 1998 to now. So I copy-pasted them all into a text file, and wrote a quick and dirty script to count their appearances and sort them – thus getting the lists of the most frequent numbers and the least frequent ones.

First, the input text file – I’ll show you a brief excerpt:

2007-07-01 29 39 15 14 23 9
2007-07-08 8 41 46 12 4 17
2007-07-15 36 41 44 46 48 10
2007-07-22 24 47 46 30 9 23
2007-07-29 43 19 13 46 38 42
2007-08-05 44 4 38 28 37 35
2007-08-12 42 43 18 47 46 44

Now, the code:

# initialize: the data structure to store the results
(1..49).each {|x| freqs[x]=0}

# parse the input text file(shown above)
res=[]"loto07.txt").each { |line|
  res[i]=line.split(" ")

#increase the frequency of appearance, for each winning number

# sort the datastructure by frequency and display
puts freqs.sort{|a,b| a[1]<=>b[1]}.inspect

Nothing more to explain: a simple ruby script to sort the winning lotto numbers across history by their frequency. Now, if you win the lotto by using this script as well, I won’t mind a small(or bigger) gift. Seriously.

If you are interested in web, databases and the like then you probably already heard of SQLITE. If not, here’s a quick intro
SQLITE is a database engine that uses just one file per database. It’s tiny, simple to use and, most importantly, very easy to use with Ruby on Rails. All you need is the sqlite ruby gem, which is already included in most Ruby installations.