Always index your tables. Always


This is going to be brief one: iRead.ro, 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.

Continue reading “Always index your tables. Always”

10 Ruby programming tips you should already know

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.

Continue reading “10 Ruby programming tips you should already know”

Enumerable iterators (why I love Ruby part 2)

Got myself a sleepless night recently and killed some time reading the Rubyisms in Rails book. The chapter about Iterators was particularly pleasant, reminding me the beauty of Ruby’s Enumerable class – feels so Lisp-ish and Scheme-ish to write stuff like col.select{..}.collect{..}.join(”, “)
Continue reading “Enumerable iterators (why I love Ruby part 2)”

Ruby Metaprogramming part 2

Last time I discussed Ruby and metaprogramming, I was trying to stay DRY (Don’t Repeat Yourself) while coding some very similar-looking methods. The solution, then was to use class_eval to dynamically add methods into the current class, the way attr_accessor and it’s peers already do.
Continue reading “Ruby Metaprogramming part 2”

*nix tip of the day

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.

Ruby for daily chores: Extracting most frequent loto numbers

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
freqs={}
(1..49).each {|x| freqs[x]=0}

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

#increase the frequency of appearance, for each winning number
res.each{|line|
  line[1..-1].each{|a|
    nb=a.to_i
    freqs[nb]=freqs[nb]+1
  }
}

# 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.

Beginner’s blogging guide – How to bring traffic to your blog

I’ve started and (rather) successfully managed around 7 blogs until now. Each attempt has been a bit more successful than the previous one, each building on the shoulders of its predecessors. So, since nothing beats a little bulleted list, here they are – top ways to bring traffic to your freshly launched blog:

Continue reading “Beginner’s blogging guide – How to bring traffic to your blog”

Setting up Rails and SQLITE

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.
Continue reading “Setting up Rails and SQLITE”