【发布时间】:2014-05-02 07:43:12
【问题描述】:
我试图找出用户输入的是字符串还是整数。如果我根据用户输入的要求输入一个固定值,我可以找到它。我无法获得它的课程。
这行得通:
name = 5
type = name.class
这不起作用:
print "Enter the value to findout its class "
name = gets
type = name.class
if type == String
puts "this is a string"
elsif type == Fixnum
puts "this is a fixnum"
else
puts "this is unknown type"
end
【问题讨论】:
-
#gets总是给你字符串...顺便说一句 -
确实如此。当我输入 gets.to_i 时,它总是给出整数。那么获取输入内容的方法是什么?
-
这取决于你想要验证的类型......
-
如果要查看字符串是否为数字,请看这里:stackoverflow.com/questions/5661466/…
-
我想知道输入是fixnum、bignum、string还是float
标签: ruby