4 Replies - 3753 Views - Last Post: 14 May 2009 - 09:38 PM Rate Topic: -----

#1 mmzdaniel   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 :ph34r:

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?

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

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

Re: Distinguish between string and number?

Posted 27 April 2009 - 03:31 PM

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

Was This Post Helpful? 1
  • +
  • -

#3 mmzdaniel   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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...
Was This Post Helpful? 0
  • +
  • -

#4 xclite   User is offline

  • I wrote you an code
  • member icon


Reputation: 1280
  • View blog
  • Posts: 4,088
  • Joined: 12-May 09

Re: Distinguish between string and number?

Posted 12 May 2009 - 04:21 PM

View Postmmzdaniel, 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.
Was This Post Helpful? 0
  • +
  • -

#5 dptorrac   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1