# Please Forward: Ruby Quiz Submission

**URL:** <https://rubytalk.org/t/please-forward-ruby-quiz-submission/40701>\
**Category:** ruby-talk\
**Created:** [18 September 2007 00:49 UTC](https://rubytalk.org/t/please-forward-ruby-quiz-submission/40701 "2007-09-18T00:49:55Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [18 September 2007 00:49 UTC](https://rubytalk.org/t/please-forward-ruby-quiz-submission/40701/1 "2007-09-18T00:49:55Z")

</div>

> 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  
> &nbsp;&nbsp;require 'sqlite3'
> 
> &nbsp;&nbsp;def initialize  
> &nbsp;&nbsp;&nbsp;&nbsp;@db = SQLite3::Database.new('ip2country.db')  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;  
> &nbsp;&nbsp;def ip2num(ipstr)  
> &nbsp;&nbsp;&nbsp;&nbsp;ipsplit = ipstr.split(".")  
> &nbsp;&nbsp;&nbsp;&nbsp;ipsplit[0].to\_i \* 256 \* 256 \* 256 +  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ipsplit[1].to\_i \* 256 \* 256 +  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ipsplit[2].to\_i \* 256 +  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ipsplit[3].to\_i  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def get\_cc(ipstr)  
> &nbsp;&nbsp;&nbsp;&nbsp;numstr = ip2num(ipstr).to\_s  
> &nbsp;&nbsp;&nbsp;&nbsp;@db.execute("SELECT CTRY FROM ITC WHERE (" + numstr +  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"IPFROM) AND (" + numstr +  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" \< IPTO);")[0][0]  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> end
> 
> if \_\_FILE\_\_ == $0  
> &nbsp;&nbsp;i2c = IpToCountry.new()  
> &nbsp;&nbsp;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
> 
> ------------------------

> **···**
>
> Begin forwarded message:
