Tuesday, July 22, 2014

Rails: try, respond_to?

User.find(5).email
- will blow up if 'email' property is not present

User.find(5).try(:email)  
- will return 'nil' if 'email' property is not present


respond_to? is a Ruby method for detecting whether the class has a particular method on it.


@user.respond_to?('eat_food')

No comments:

Post a Comment

Followers

Blog Archive