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

前端 未结 7 1626
礼貌的吻别
礼貌的吻别 2020-12-13 11:59

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

7条回答
  •  我在风中等你
    2020-12-13 12:43

    There are several ways:

    >> 1.class #=> Fixnum
    >> "foo".class #=> String
    >> 1.is_a? Numeric #=> true
    >> "foo".is_a? String #=> true
    

提交回复
热议问题