Last active
December 12, 2015 03:09
-
-
Save burtlo/4704867 to your computer and use it in GitHub Desktop.
This is code for checking the validity of phone numbers for Event Manager
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def check_valid_phone_number(phone_number) | |
number_length = phone_number.length | |
if number_length == 10 | |
phone_number | |
elsif number_length == 11 and phone_number[0] == '1' | |
phone_number[1..10] | |
else | |
invalid_number | |
end | |
end | |
def invalid_number | |
'Invalid number' | |
end |
Second, you could combine the nested if statements for the length 11 numbers that start with a '1'
Third, when the number is less than 10 and greater than 10 it is valid number so you might want to just rely on your number_length
explicit checks of 10 and 11.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is great because it reduces the the use of the 'Invalid number' string in multiple places.