def find_npa(dialed_nxx)
nxx_819 = [ 205, 208, 210, 213, 243 ]
nxx_613 = [ 203, 204, 212, 216, 218 ]
nxx_819.each do |nxx|
if nxx == dialed_nxx
return 819
end
end
nxx_613.each do |nxx|
if nxx == dialed_nxx
return 613
end
end
return
end
if npa = find_npa(204)
print npa
end
Refactorings
No refactoring yet !
danielharan
June 12, 2008, June 12, 2008 00:02, permalink
def find_npa(dialed_nxx) return 819 if [ 205, 208, 210, 213, 243 ].include? dialed_nxx return 613 if [ 203, 204, 212, 216, 218 ].include? dialed_nxx end
elliottcable
June 15, 2008, June 15, 2008 04:50, permalink
A a bit more versatile, but a bit less clean, than Daniel's. Also allows an arbitrary number of NXX's later (though I have no idea what an NXX is).
def npa_for nnx
{
819 => [ 205, 208, 210, 213, 243 ],
613 => [ 203, 204, 212, 216, 218 ]
}.select {|k,v| v.include? nnx }.flatten.first
end
I'm new to Ruby, so give me some good examples of how to do this differently