67

How to check if a variable is a number or a string in Ruby?

prusswan
  • 6,853
  • 4
  • 40
  • 61
Leem.fin
  • 40,781
  • 83
  • 202
  • 354

7 Answers7

101

There are several ways:

>> 1.class #=> Fixnum
>> "foo".class #=> String
>> 1.is_a? Numeric #=> true
>> "foo".is_a? String #=> true
Michael Kohl
  • 66,324
  • 14
  • 138
  • 158
  • 11
    This doesn't check whether the *variable* is a number, it checks whether the object the variable *points to* is a number. There is a *huge* difference between the two. – Jörg W Mittag Dec 23 '11 at 13:47
  • 34
    Given how questions on SO (and elsewhere) are colloquially asked, it's fair to assume that's what OP meant, especially since he or she accepted it. – Michael Kohl Dec 23 '11 at 14:19
31
class Object
  def is_number?
    to_f.to_s == to_s || to_i.to_s == to_s
  end
end

> 15.is_number?
=> true
> 15.0.is_number?
=> true
> '15'.is_number?
=> true
> '15.0'.is_number?
=> true
> 'String'.is_number?
=> false
installero
  • 9,096
  • 3
  • 39
  • 43
  • 1
    This worked for me where `.is_a? Numeric` did not. I was checking if a field extracted from a CSV file was a number, thus comparison to `Numeric` fails because it is actually a string that also happens to be a number. – Scott Weldon Feb 25 '15 at 19:01
  • 4
    This fails in a few cases, like `15.10`, `015` or `15.33333333333333333`. – Andrew Taylor Apr 15 '15 at 14:31
  • The following definition for is_number? satsifies Andrew's concerns: def is_number?(text) !!(/^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/ =~ text) end – Ben Hocking Oct 30 '16 at 21:09
19
var.is_a? String

var.is_a? Numeric
Christoph Geschwind
  • 2,602
  • 1
  • 13
  • 15
  • 5
    This doesn't check whether the *variable* is a number, it checks whether the object the variable *points to* is a number. There is a *huge* difference between the two. – Jörg W Mittag Dec 23 '11 at 13:48
  • 23
    @Jörg: I think it is pretty obvious what the OP is asking, there's no need for pedantry in this case. – mu is too short Dec 23 '11 at 18:29
  • 1
    @muistooshort well, not really. I came here looking for a way to _test_ a variable to check if it contained a string or a number value. So it's not pretty obvious to me, at least. – Sergio A. Oct 13 '20 at 14:30
5

The finishing_moves gem includes a String#numeric? method to accomplish this very task. The approach is the same as installero's answer, just packaged up.

"1.2".numeric?
#=> true

"1.2e34".numeric?
#=> true

"1.2.3".numeric?
#=> false

"a".numeric?
#=> false
Frank Koehl
  • 3,104
  • 2
  • 29
  • 37
3
class Object
  def numeric?
    Float(self) != nil rescue false
  end
end
markhorrocks
  • 1,199
  • 19
  • 82
  • 151
2

Print its class, it will show you which type of variable is (e.g. String or Number).

e.g.:

puts varName.class
Scott Weldon
  • 9,673
  • 6
  • 48
  • 67
BSalunke
  • 11,499
  • 8
  • 34
  • 68
  • 1
    This doesn't check whether the *variable* is a number, it checks whether the object the variable *points to* is a number. There is a *huge* difference between the two. – Jörg W Mittag Dec 23 '11 at 13:48
-4
if chr.to_i != 0
  puts "It is number,  yep"
end
Scott Weldon
  • 9,673
  • 6
  • 48
  • 67
Liker777
  • 2,156
  • 4
  • 18
  • 25