I recently came across a need for a project where I had to write a check in the app’s backend to check if the mobile number being sent by the client matched a certain pattern. The pattern required:

  1. The string must be 13 characters long
  2. The string must start with +
  3. All other characters must be digits

While you can write a regular expression, even without one, the checks are easy and can be written as 3 if-else conditions. I did write a regular expression but for a particular input, the expression failed. I then had to replace the regex with 3 if-else conditions. That is when I discovered that the input string was not qualifying the first condition - the string length condition.

Below, I present the code in Ruby. Can you look at the code and tell what is wrong with it?

If you figure out what is wrong, share the problem with others and please let others know in the comment section how you solved it.

If you are thinking, this is a ruby specific problem, here is the same problem in Javascript:

PS: I have already solved the problem and it took me about 6 hours of rigorous debugging before I could find the answer.