# Area Codes

**URL:** <https://www.ruby-forum.com/t/area-codes/182976>\
**Category:** Ruby\
**Created:** [February 5, 2010, 3:32pm UTC](https://www.ruby-forum.com/t/area-codes/182976 "2010-02-05T15:32:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![woodyee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/43a26b/32.png) [@woodyee](https://www.ruby-forum.com/u/woodyee)\
**Post date:** [February 5, 2010, 3:32pm UTC](https://www.ruby-forum.com/t/area-codes/182976/1 "2010-02-05T15:32:59Z")

</div>

Hi! TOTAL noob question here but that’s what I am. I’m trying to create  
a program where I type the area code, hit enter, and see it’s state. I  
have a list of codes per state and it’s rather large. How’s the best way  
to do this? I don’t want to use a database (MySQL) for this if that’s  
possible. I’ve tried using if statements and hashes but didn’t know if  
there’s a better way. I’ve searched for a gem that’s already written but  
didn’t have any luck and I’ve googled to see if there’s source code for  
this already but couldn’t find anything. Any ideas? Thanks!

---

<div class="post-metadata">

**Author:** ![woodyee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/43a26b/32.png) [@woodyee](https://www.ruby-forum.com/u/woodyee)\
**Post date:** [February 5, 2010, 8:50pm UTC](https://www.ruby-forum.com/t/area-codes/182976/2 "2010-02-05T20:50:48Z")

</div>

Wood Y. wrote:

> Hi! TOTAL noob question here but that’s what I am. I’m trying to create  
> a program where I type the area code, hit enter, and see it’s state. I  
> have a list of codes per state and it’s rather large. How’s the best way  
> to do this? I don’t want to use a database (MySQL) for this if that’s  
> possible. I’ve tried using if statements and hashes but didn’t know if  
> there’s a better way. I’ve searched for a gem that’s already written but  
> didn’t have any luck and I’ve googled to see if there’s source code for  
> this already but couldn’t find anything. Any ideas? Thanks!

Use a database (if this is a desktop application, SQLite would work  
well; if you’re using JRuby, try H2). It’s the best way. If you use  
one of the many excellent ORM libraries (ActiveRecord, Sequel,  
DataMapper…), this is quite easy.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![woodyee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/43a26b/32.png) [@woodyee](https://www.ruby-forum.com/u/woodyee)\
**Post date:** [February 5, 2010, 9:55pm UTC](https://www.ruby-forum.com/t/area-codes/182976/3 "2010-02-05T21:55:03Z")

</div>

On Feb 5, 2010, at 9:33 AM, Wood Y. wrote:

> Hi! TOTAL noob question here but that’s what I am. I’m trying to create  
> a program where I type the area code, hit enter, and see it’s state. I  
> have a list of codes per state and it’s rather large. How’s the best way  
> to do this? I don’t want to use a database (MySQL) for this if that’s  
> possible. I’ve tried using if statements and hashes but didn’t know if  
> there’s a better way. I’ve searched for a gem that’s already written but  
> didn’t have any luck and I’ve googled to see if there’s source code for  
> this already but couldn’t find anything. Any ideas? Thanks!

There are lots of ways to do this. Maybe this script will give you  
some ideas. This uses Ruby’s ability to embed data at the end of  
the script but you could also store the data in an external file.

$ cat areacode.rb  
BEGIN {  
@info = {}  
DATA.each\_line do |x|  
code, state = x.split  
@info[code] = state  
end  
@state = Hash.new { |h,k| @info[k.to\_s] }  
}

[203,914,213].each { |c|  
puts “area code #{c} is in #{@state[c]}”  
}

# Put your area code data below

**END**  
203 CT  
860 CT  
914 NY  
213 CA  
$ ruby areacode.rb  
area code 203 is in CT  
area code 914 is in NY  
area code 213 is in CA
