How To Check If A Class Or Instance Has A Method Defined In Ruby

Posted by Weston Ganger on August 25, 2016

When writing my plugin Spreadsheet Architect I needed to check if methods were defined on both the class and instances. Heres two fast ways to do this:

Post.respond_to?(:item_name) #=> false
@post.respond_to?(:item_name) #=> true

# Check if the instance method is defined
Post.method_defined?(:some_instance_method) #=> false
@post.method_defined?(:some_instance_method) #=> true

Related External Links:

Posted in Rails and Tagged with ruby 

Need help on your next project or application?

I specialize in Ruby-on-Rails, AngularJS, Javascript, Bootstrap, and Hybrid Mobile Apps with Cordova & Ionic.

Contact Me

Recommended Posts