Given a class, see if instance has method (Ruby)

前端 未结 12 1805

I know in Ruby that I can use respond_to? to check if an object has a certain method.

But, given the class, how can I check if the instance has a certai

12条回答
  •  时光说笑
    2020-12-12 12:43

    On my case working with ruby 2.5.3 the following sentences have worked perfectly :

    value = "hello world"
    
    value.methods.include? :upcase
    

    It will return a boolean value true or false.

提交回复
热议问题