[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Fwd: Please Forward: Ruby Quiz Submission

James Gray

9/18/2007 12:50:00 AM

Begin forwarded message:

> From: Kevin Winchester <kjwinchester@gmail.com>
> Date: September 17, 2007 7:29:13 PM CDT
> To: submission@rubyquiz.com
> Subject: Please Forward: Ruby Quiz Submission
>
>
> Hi,
>
> Here's my first attempt at a ruby quiz. I wasn't going to bother
> submitting it, until I realised that no one else did the same thing I
> did.
>
> I took the CSV file and converted it into an SQLite database and used
> sqlite-ruby to perform a selection.
>
> In the process, I noticed that there is actually one duplicate
> starting IP address in the CSV file (which I simply removed as its
> range
> was entirely within the range of the next line).
>
> So I end up with a very short ruby program, at least, although my
> times
> are still a little longer than some of the other submissions.
>
> --------------------------
>
> class IpToCountry
> require 'sqlite3'
>
> def initialize
> @db = SQLite3::Database.new('ip2country.db')
> end
>
> def ip2num(ipstr)
> ipsplit = ipstr.split(".")
> ipsplit[0].to_i * 256 * 256 * 256 +
> ipsplit[1].to_i * 256 * 256 +
> ipsplit[2].to_i * 256 +
> ipsplit[3].to_i
> end
>
> def get_cc(ipstr)
> numstr = ip2num(ipstr).to_s
> @db.execute("SELECT CTRY FROM ITC WHERE (" + numstr +
> "IPFROM) AND (" + numstr +
> " < IPTO);")[0][0]
> end
> end
>
> if __FILE__ == $0
> i2c = IpToCountry.new()
> puts i2c.get_cc(ARGV[0])
> end
>
> ------------------------
>
> kevin@alekhine:~/ruby/rubyquiz/q139$ time ruby ip_to_country.rb
> 68.97.89.187
> US
>
> real 0m0.095s
> user 0m0.055s
> sys 0m0.005s
>
> ------------------------