Check whether a variable is a string in Ruby

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

Is there anything more idiomatic than the following?

foo.class == String
6条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-30 02:22

    In addition to the other answers, Class defines the method === to test whether an object is an instance of that class.

    • o.class class of o.
    • o.instance_of? c determines whether o.class == c
    • o.is_a? c Is o an instance of c or any of it's subclasses?
    • o.kind_of? c synonym for *is_a?*
    • c === o for a class or module, determine if *o.is_a? c* (String === "s" returns true)

提交回复
热议问题