ruby’s respond_to? for checking if a method exists

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

This came in very handy the other day when I wanted to send emails in muziboo to people whenever they get a reply for either their posts, or a comment on their song or other stuff. Since I am using acts_as_commentable for all commenting, I added this to comments.rb (model)

after_create
if commentable.respond_to?('notify_comment')
commentable.notify_comment()
end
end

Now every model, for which I wanted to send an email (when a new comment is added), i defined notify_comment() function and called UserNotifier.deliver_xxx method inside that. For models where I did not want to send an email, I never defined the function and everything worked fine because respond_to? will return a false for those models

The above really helped me simplify the controller code and keep the code consistent. Thought of sharing this with everyone. Please do leave your comments if something is not quite right :)

Popularity: 42% [?]

find this blog feedworthy. Click on the number to subscribe!

Related Posts

Move to Rails 2.0.2

Active record validation with mongrel_upload_progress

Comments

No comments yet.

Leave a comment

(required)

(required)