DEV Community 👩‍💻👨‍💻


Posted on

How to check if a particular method exists in a Ruby class and instance

Ruby: 2.5.1

Checking the existence of a method for a class

Using method_defined?

=> true

# I see that there is a to_i method, so I'm going to run it
=> 1

Does the method exist for the instance?

Using respond_to?

=> false

# There is no to_a method so I get a NoMethodError error when I try to run it

Latest comments (0)

Take a look at this:


Go to your customization settings to nudge your home feed to show content more relevant to your developer experience level. 🛠