Category: Programming

Cum sa programezi un joc in sub 5 minute

Nu eu am facut asa ceva, dar performanta acestui om mi se pare extraordinara si inspirationala.
In 4 minute si 30 de secunde, el a reusit sa codeze in Javascript un prototip functional, jucabil, ba chiar bun (dupa standardele de acum 40 de ani) a celebrului Snake. Totul, in timp ce vorbeste si explica ce face.


Pe scurt si in rezumat, prototipurile se fac repede, totul este sa vrei. Si sa te pricepi. Si sa incerci. Si, in aceste conditii, se fac repede.

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.
(more…)

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.

(more…)

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.
(more…)

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.

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.
(more…)