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

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

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

相关标签:
7条回答
  • 2020-12-13 12:55

    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
    
    0 讨论(0)
提交回复
热议问题