# Distinguish between string and number?

Page 1 of 1

## 4 Replies - 3738 Views - Last Post: 14 May 2009 - 09:38 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=101922&amp;s=ea7bcb0c59c86afc54c29d4c89dfdbc3&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 mmzdaniel

Reputation: 0
• Posts: 5
• Joined: 17-January 09

# Distinguish between string and number?

Posted 27 April 2009 - 08:54 AM

Ive been trying to make an app, that takes an input... what the app really does, doesn't really matter as my problem is... how do I make my program to know if the user entered a string or a number? so far i made this:
```#This function is just a factorial function.
def fact(n)
a = 1
b = 1
while a <= n
b = b * a
a += 1
end
end
#And this is the function I'm talking about. Its the best i could think of...
def num?(n)
true if (fact(n) != 0) rescue false
end
```

problem is, the program uses numbers and 0 should be considered a number as well... with this function its not possible.
What I'm asking for is an efficient function to give me a true value if the user got a number and false if its a string...
Pseudocode would be preferable but the function alone would be fine too i guess...
Thanks.

P.S.
This is a ninja

This post has been edited by mmzdaniel: 27 April 2009 - 08:56 AM

Is This A Good Question/Topic? 0

## Replies To: Distinguish between string and number?

• Saucy!

Reputation: 6246
• Posts: 24,014
• Joined: 23-August 08

## Re: Distinguish between string and number?

Posted 27 April 2009 - 03:31 PM

```def num?(input)
return input.is_a? Integer
end
```

### #3 mmzdaniel

Reputation: 0
• Posts: 5
• Joined: 17-January 09

## Re: Distinguish between string and number?

Posted 29 April 2009 - 09:47 AM

Thanks mate, that works awesome... i just changed Integer to Float, as im using floats... i recall trying to do it but i didn't know just placing "return" without WHAT to return (true or false) would actually work...

### #4 xclite

• I wrote you an code

Reputation: 1264
• Posts: 4,062
• Joined: 12-May 09

## Re: Distinguish between string and number?

Posted 12 May 2009 - 04:21 PM

mmzdaniel, on 29 Apr, 2009 - 08:47 AM, said:

Thanks mate, that works awesome... i just changed Integer to Float, as im using floats... i recall trying to do it but i didn't know just placing "return" without WHAT to return (true or false) would actually work...

As for telling the script what to return, a Ruby method returns the value of its last operation.

### #5 dptorrac

Reputation: 0
• Posts: 1
• Joined: 14-May 09

## Re: Distinguish between string and number?

Posted 14 May 2009 - 09:38 PM

an additional way to ensure that what you're working with is correct is to use to_i/to_s...etc, look at the api. This will make sure that whatever you are working with is treated as the given variable type.

Also pretty sure that if you want to make sure it's simply a type of number, instead of is_a? Integer or float or whatever. You can use is_a? FixNum, which is the parent class of all the numbers. However...not completely sure about that...been a while and my Ruby is rusty