Skip to content

Instantly share code, notes, and snippets.

@burtlo
Last active December 12, 2015 03:09
Show Gist options
  • Save burtlo/4704867 to your computer and use it in GitHub Desktop.
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
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
@burtlo
Copy link
Author

burtlo commented Feb 4, 2013

This is great because it reduces the the use of the 'Invalid number' string in multiple places.

@burtlo
Copy link
Author

burtlo commented Feb 4, 2013

Second, you could combine the nested if statements for the length 11 numbers that start with a '1'

@burtlo
Copy link
Author

burtlo commented Feb 4, 2013

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