Check whether a variable is a string in Ruby

后端 未结 6 614
陌清茗
陌清茗 2021-01-30 01:58

Is there anything more idiomatic than the following?

foo.class == String
6条回答
  •  别那么骄傲
    2021-01-30 02:43

    foo.instance_of? String
    

    or

    foo.kind_of? String 
    

    if you you only care if it is derrived from String somewhere up its inheritance chain

提交回复
热议问题