Project

tryit

0.0
No commit activity in last 3 years
No release in over 3 years
Try methods without exceptions
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
~> 2.9.0
 Project Readme

Tryit

Another approach to Rail's Object#try. This is the result of a StackOverflow discussion between Sergey Gopkalo and Michael Kohl.

Instead of

obj.try(:met1).try(:met2).try(:met3).to_s

you can do this

obj.tryit { met1.met2.met3.to_s }

or this (the preferred form):

tryit { obj.met1.met2.met3.to_s }

You can customize which exception to catch:

TryIt.exceptions << ZeroDivisionError
tryit { 1/0 }  # will not raise exceptions

There's also the possibility to define your own exception handlers:

TryIt.handler = lambda { |_| puts "message from tryit" }
tryit { raise NoMethodError } # will print "message from tryit"

Installation

Add this line to your application's Gemfile:

gem 'tryit'

And then execute:

$ bundle

Or install it yourself:

$ gem install tryit

Todo

  • Find a way to make the exception list not global.

License

Lincesend under the MIT license. See the provided LICENSE file for details.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request