Checking if a variable is an integer

后端 未结 11 1600
小鲜肉
小鲜肉 2020-12-07 16:20

Does Rails 3 or Ruby have a built-in way to check if a variable is an integer?

For example,

1.is_an_int #=> true
\"dadadad@asdasd.net\".is_an_int          


        
11条回答
  •  长情又很酷
    2020-12-07 16:49

    You can use the is_a? method

    >> 1.is_a? Integer
    => true
    >> "dadadad@asdasd.net".is_a? Integer
    => false
    >> nil.is_a? Integer
    => false
    

提交回复
热议问题