School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,406 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,614 people online right now. Registration is fast and FREE... Join Now!




Distinguish between string and number?

 

Distinguish between string and number?

mmzdaniel

27 Apr, 2009 - 07:54 AM
Post #1

New D.I.C Head
*

Joined: 17 Jan, 2009
Posts: 5

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:
CODE
#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 ph34r.gif

This post has been edited by mmzdaniel: 27 Apr, 2009 - 07:56 AM

User is offlineProfile CardPM
+Quote Post


JackOfAllTrades

RE: Distinguish Between String And Number?

27 Apr, 2009 - 02:31 PM
Post #2

I exist to Google your problems.
Group Icon

Joined: 23 Aug, 2008
Posts: 4,948



Thanked: 424 times
Dream Kudos: 50
Expert In: Being annoyed with lazy people.

My Contributions
How about
ruby
def num?(input)
return input.is_a? Integer
end

User is offlineProfile CardPM
+Quote Post

mmzdaniel

RE: Distinguish Between String And Number?

29 Apr, 2009 - 08:47 AM
Post #3

New D.I.C Head
*

Joined: 17 Jan, 2009
Posts: 5

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...
User is offlineProfile CardPM
+Quote Post

xclite

RE: Distinguish Between String And Number?

12 May, 2009 - 03:21 PM
Post #4

LIKE A BOSS
****

Joined: 12 May, 2009
Posts: 629



Thanked: 13 times
My Contributions
QUOTE(mmzdaniel @ 29 Apr, 2009 - 08: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...


As for telling the script what to return, a Ruby method returns the value of its last operation.
User is offlineProfile CardPM
+Quote Post

dptorrac

RE: Distinguish Between String And Number?

14 May, 2009 - 08:38 PM
Post #5

New D.I.C Head
*

Joined: 14 May, 2009
Posts: 1

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

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 10:45PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month