|
This post originated from an RSS feed registered with Ruby Buzz
by rwdaigle.
|
Original Post: What's New in Edge Rails: Object.try
Feed Title: Ryan's Scraps
Feed URL: http://feeds.feedburner.com/RyansScraps
Feed Description: Ryan Daigle's various technically inclined rants along w/ the "What's new in Edge Rails" series.
|
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by rwdaigle
Latest Posts From Ryan's Scraps
|
|
Those of you using Chris Wanstrath’s slick little try trick will now have access to that functionality in Rails with this ActiveSupport update.
Basically, try lets you attempt to invoke a method on an object without worrying about a NoMethodError being raised. If the method doesn’t exist, or if the target object nil, then nil will be returned without exceptions:
1
2
3
4
5
|
# No exceptions when receiver is nil
nil.try(:destroy) #=> nil
# Useful when chaining potential nil items
User.admins.first.try(:address).try(:reset) |
Just a small little bit of syntactical candy pulled in from the community.
tags: ruby,
rubyonrails

Read: What's New in Edge Rails: Object.try