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

Posted By Weston Ganger

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:

Article Topic:Software Development - Ruby / Rails

Date:August 25, 2016