DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Check If A Ruby Method Exists

03.14.2010
| 21739 views |
  • submit to reddit
        Source: <a href="http://www.prateekdayal.net/2007/10/16/rubys-responds_to-for-checking-if-a-method-exists/">ruby’s respond_to? for checking if a method exists</a> [prateekdayal.net]

<snip>
Ruby has a function called respond_to? that can be used to seeing if a particular class or object has a method with a certain name. The syntax is something like
User.respond_to?('name') # returns true is method name exists otherwise false
</snip>