2

I googled that there is an is_a? function to check whether an object is an integer or not.

But I tried in rails console, and it doesn't work.

I ran the code like the following:

 "1".is_a?
 1.is_a?

Did I miss something?

Nakilon
  • 34,866
  • 14
  • 107
  • 142
mlzboy
  • 14,343
  • 23
  • 76
  • 97
  • 1
    Google is nice, but it's not the first place to look for what is in Ruby's standard and core libraries. I'd recommend going to the primary source: your own machine's `ri`, or the [rubydoc site](http://www.ruby-doc.org/). Entering `ri is_a?` at your command-line could return a couple hits, with the important one being `Object.is_a?` – the Tin Man Nov 26 '10 at 03:31
  • Related question for what you're trying to do: [Retrieve number from the string pattern using regular expression](http://stackoverflow.com/questions/694176/retrieve-number-from-the-string-pattern-using-regular-expression) – Andrew Grimm Nov 26 '10 at 07:53

6 Answers6

19

You forgot to include the class you were testing against:

"1".is_a?(Integer) # false
1.is_a?(Integer) # true
Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
  • thanks, in fact my situation is i want to check a string is a number,if "1".is_a?(Ingeger) return false,it seems didn't useful for this situation – mlzboy Nov 26 '10 at 03:33
  • 1
    @mlzboy: As far as I know, Ruby treats `"1"` as a string, rather than as a potential integer. You have to do `2 + "2".to_i` or `2 + Integer(2)` - you can't do `2 + "2"`. You may want to read [Is ruby strongly or weakly typed ?](http://stackoverflow.com/questions/520228/is-ruby-strongly-or-weakly-typed) – Andrew Grimm Nov 26 '10 at 07:28
3

There's not a built in function to say if a string is effectively an integer, but you can easily make your own:

class String
  def int
    Integer(self) rescue nil
  end
end

This works because the Kernel method Integer() throws an error if the string can't be converted to an integer, and the inline rescue nil turns that error into a nil.

Integer("1") -> 1
Integer("1x") -> nil
Integer("x") -> nil

and thus:

"1".int -> 1 (which in boolean terms is `true`)
"1x".int -> nil
"x".int -> nil

You could alter the function to return true in the true cases, instead of the integer itself, but if you're testing the string to see if it's an integer, chances are you want to use that integer for something! I very commonly do stuff like this:

if i = str.int
  # do stuff with the integer i
else
  # error handling for non-integer strings
end

Although if the assignment in a test position offends you, you can always do it like this:

i = str.int
if i
  # do stuff with the integer i
else
  # error handling for non-integer strings
end

Either way, this method only does the conversion once, which if you have to do a lot of these, may be a significant speed advantage.

[Changed function name from int? to int to avoid implying it should return just true/false.]

glenn mcdonald
  • 15,290
  • 3
  • 35
  • 40
  • Just to be clear, I didn't see your edit until after I posted this, and it wasn't me who downvoted you. – glenn mcdonald Nov 26 '10 at 05:05
  • 1
    -1 because this has nothing to do with the question and because this is a horrible idea. ? methods should return a boolean. 1 is not equal to true. – Samuel Nov 26 '10 at 06:55
  • Pertains exactly to the question, as the poster clarified in response to Andrew Grimm. Read the whole page before writing, next time. mlzboy, feel free to call your method `int` instead of `int?` if you prefer methods with ? to only return strict booleans. Or any other name you like. (It's his method, I'm just showing him how to do it.) – glenn mcdonald Nov 26 '10 at 13:54
  • I changed it to `int`. The `?` really wasn't the important part. – glenn mcdonald Nov 27 '10 at 03:02
1

i used a regular expression

if a =~ /\d+/
   puts "y"
else
   p 'w'
end
mlzboy
  • 14,343
  • 23
  • 76
  • 97
0

Ruby has a function called respond_to? that can be used to seeing if a particular class or object has a method with a certain name. The syntax is something like

User.respond_to?('name') # returns true is method name exists
otherwise false

http://www.prateekdayal.net/2007/10/16/rubys-responds_to-for-checking-if-a-method-exists/

E3pO
  • 493
  • 1
  • 9
  • 21
0

Maybe this will help you

str = "1"
=> "1"
num = str.to_i
=> 1
num.is_a?(Integer)
=> true

str1 = 'Hello'
=> "Hello"
num1 = str1.to_i
=> 0
num1.is_a?(Integer)
=> true
Rohit
  • 5,631
  • 4
  • 31
  • 59
  • Both `"1"` and `'hello'` ended up with `true`. Did you mean to do that? – Andrew Grimm Nov 26 '10 at 07:45
  • @Andrew I know that both ended up with true. But I thought there is a difference in that if a string is converted to integer as in "Hello" we get 0 as the value. So this might be the distinguishing point. – Rohit Nov 26 '10 at 09:56
  • From the String#to_i docs: If there is not a valid number at the start of str, 0 is returned. – Joshua Cheek Nov 26 '10 at 16:09
0

I wanted something similar, but none of these did it for me, but this one does - use "class":

a = 11
a.class
=> Fixnum
rtfminc
  • 6,243
  • 7
  • 36
  • 45