New method Object#attempt, similar to Object#try, except returns self instead of nil if method does not exist
16 views
Skip to first unread message
jeffrey....@chargeback.com
unread,
Mar 8, 2017, 4:44:53 PM3/8/17
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Sign in to report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Ruby on Rails: Core
I have run into many cases where I need to attempt to run the method, however if the method does not exist, I would prefer to get the object back, instead of nil.
This is the change I am proposing
moduleActiveSupport moduleTryable#:nodoc: deftry(*a,&b) try!(*a,&b)if a.empty?|| respond_to?(a.first) end
# NEW METHOD def attempt(*a,&b) try(*a,&b)||self end end end