检查变量是否为Ruby中的字符串 [英] Check whether a variable is a string in Ruby
本文介绍了检查变量是否为Ruby中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
还有什么比下面的惯用的了吗?
Is there anything more idiomatic than the following?
foo.class == String
推荐答案
我认为您正在寻找 instance_of?
.对于来自派生类的实例, is_a?
和 kind_of?
将返回true.
I think you are looking for instance_of?
. is_a?
and kind_of?
will return true for instances from derived classes.
class X < String
end
foo = X.new
foo.is_a? String # true
foo.kind_of? String # true
foo.instance_of? String # false
foo.instance_of? X # true
这篇关于检查变量是否为Ruby中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文