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 

Want me to help develop your next project or application?

Contact Us

Specializing in Ruby-on-Rails, Javascript, Multi-Platform Electron Desktop Apps and Hybrid Cordova Mobile Apps.

Recommended Posts