Endeavour is a simple gem that adds a try
method to Object
and NilClass
instances. try
permits calling a method on an object and returning nil
rather than NoMethodError
if the object is nil
or does not respond to
the method.
There are two ways to use try
:
1. Call-based syntax (similar to Rails' Object#try)
car.try(:drive)
colors.try(:[], :red)
1. Delegate syntax (inspired by HoboSupport)
car.try.drive
colors.try[:red]
Add this line to your application's Gemfile:
gem 'endeavour'
And then execute:
$ bundle
Or install it yourself as:
$ gem install endeavour
Endeavour uses a Railtie and can be used after initialization without additional configuration.
Call Endeavour.hook!
early in your application.
In some cases, you may want to remove Endeavour (say, if a naughty gem requires
it). You can do so with Endeavour.remove_hook!
Bug reports and pull requests are welcome on GitHub at https://github.com/allspiritseve/endeavour.
The gem is available as open source under the terms of the MIT License.