-2

I found this code on another thread.

def is_number? string
  true if Float(string) rescue false
end

Instead of using a method to return true or false, I'd like to do this "is_numeric" test in one line, in an if statement. Can someone explain if this is possible? I'm getting errors at the moment both when the string variable is null and when it contains non-numeric characters.

if Float(string)
  * do something
else
  * do something else
end
Community
  • 1
  • 1
John Owens
  • 61
  • 6
  • 1
    I don't see what's wrong with `true if Float(string) rescue false`. It seems to be doing what you are asking. Am I misunderstanding the question? – Kimball Jul 15 '15 at 22:56
  • You can't use `if` in this example because something like `Float("blah")` will raise an exception. – Powers Jul 16 '15 at 00:43
  • @Kimball - i was trying to use the if structure in combination with this Float(string) test. In my application, a method is not a clean way of doing this, a single line would be better. – John Owens Jul 19 '15 at 10:16
  • @Powers - that's the problem which has caused me to ask this question. The method structure uses rescue for the exception. How would that transfer when using the if structure? – John Owens Jul 19 '15 at 10:16
  • @JohnOwens it's not clear what you want. Do you want to test whether string is a number? Do you want to test whether string contains numeric characters? Please explain. – La-comadreja Jul 19 '15 at 16:49
  • Since you want to use `Float(string)` and don't/can't trust the input, you need to use `rescue`. Are you after [this question](http://stackoverflow.com/questions/2524740/in-ruby-what-structures-can-a-rescue-statement-be-nested-in)? – Kimball Jul 20 '15 at 00:27
  • @Kimball - we're definitely on to something there. the begin/end structure seems like what I wanted. I'll try that. I ended up using a regex for now. – John Owens Jul 20 '15 at 18:02
  • 1
    @La-comadreja - yes, I'm testing for numeric characters, for a phone number. i ended up using a regex, but I was hoping to add to my arsenal with this type of structure. regex is probably the right way to go about what I'm doing with a phone number, but you never know where this system might be more useful. – John Owens Jul 20 '15 at 18:04
  • One problem with regex is that it is slower than other types of validations, simply due to the sheer number of calculations performed. A couple questions. First, is the running speed important to you? Second, do you want to check if there are ANY numbers in the string, whether there are a certain number of numbers in the string (e.g. U.S. area code + number is 10 digits), or if the numbers are formatted in a specific way? I'm still not clear on what you want. – La-comadreja Jul 20 '15 at 18:13
  • @La-comadreja Sorry. I haven't explained it properly at all. I'm testing the validity of a phone number. The field automatically adds brackets and hyphens as the user is entering it, but then those characters are stripped before it hits the db. This is intended as a quick check along the way to ensure that the phone number being recorded is correct. 10 digits, numbers only. – John Owens Jul 21 '15 at 19:11

2 Answers2

1

if Float() is pointless code, since Float() will either return a truthy value or raise an error (based on my limited look at the source code - as of writing, you can follow the code path from line #2942). I'd suggest you're asking the wrong question/looking at the problem wrong (it'd be helpful to know what you're actually trying to achieve).

To do something with Float() on one line and avoid breaking code, use rescue as a statement modifier, as has been done in the is_number? method posted.

Float(string) rescue 0.0 # trying to emulate String#to_f!
Kimball
  • 1,241
  • 11
  • 17
0

Ensuring the phone number is 10 digits, numbers only, is quite simple.

PHONE_NUM_LENGTH = 10
string.length == PHONE_NUM_LENGTH && string.count('0-9') == PHONE_NUM_LENGTH

will return the true/false value representing this check. This is more efficient than a Regex.

The first part,

string.length == PHONE_NUM_LENGTH

checks whether the string is 10 characters long. The second,

string.count('0-9') == PHONE_NUM_LENGTH

checks whether it has exactly 10 numeric characters.

La-comadreja
  • 5,627
  • 11
  • 36
  • 64
  • Thanks for this, but this is way off topic from the original question. I wanted to know if the Float thing could be shoe-horned into an if structure. – John Owens Jul 22 '15 at 20:42
  • what do you mean by "the Float thing could be shoe-horned into an if structure?" I don't understand what shoehorning would entail. – La-comadreja Jul 23 '15 at 02:56