Does ruby 1.9.2 have an is_a? function?

前端 未结 6 1071
忘掉有多难
忘掉有多难 2020-12-17 06:59

I googled that there is an is_a? function to check whether an object is an integer or not.

But I tried in rails console, and it doesn\'t work.

I

6条回答
  •  时光取名叫无心
    2020-12-17 07:46

    Ruby has a function called respond_to? that can be used to seeing if a particular class or object has a method with a certain name. The syntax is something like

    User.respond_to?('name') # returns true is method name exists
    otherwise false
    

    http://www.prateekdayal.net/2007/10/16/rubys-responds_to-for-checking-if-a-method-exists/

提交回复
热议问题