4b81fa52ec2103d12ecbd5993463b2b5

I'm new to Ruby, so give me some good examples of how to do this differently

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 !

880cbab435f00197613c9cc2065b4f5a

danielharan

June 12, 2008, June 12, 2008 00:02, permalink

2 ratings. Login to rate!
1
2
3
4
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
F22c48808ddcb56564889bb39f755717

elliottcable

June 15, 2008, June 15, 2008 04:50, permalink

1 rating. Login to rate!

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).

1
2
3
4
5
6
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

Your refactoring





Format Copy from initial code

or Cancel