EOr

github.com/benweissmann/eor

By Ben Weissmann, [email protected]

Adds Proc#eor, which takes an arbitrary number of procs and an optional backup value, and returns the result of the first Proc that doesn’t throw an error.

Procs recive as arguments all previous errors.

If all Procs throw errors, eor returns nil.

Usage

See the spec folder for more in-depth examples.

Basics

proc{ 'brevity' }.eor
# => "brevity"

proc { not_a_method }.eor
# => nil

With Backup Values

proc { not_a_method }.eor proc { 'is' }
# => "is"

proc { not_a_method }.eor do
  'the'
end
# => "the"

proc { not_a_method }.eor 'soul'
# => "soul"

proc { not_a_method }.eor proc {10 / 0}
# => nil

With Many Procs

proc { not_a_method }.eor proc { 10 / 0 }, proc { system }, 'of'
# => "of"

proc { not_a_method }.eor proc {10 / 0 }, proc { 'wit' }, proc { $foo = true }
# => "wit"

$foo # foo wasn't set by the previous line, becuase a successful proc was found first.
# => nil 

proc { not_a_method }.eor proc {10 / 0 }, proc { '--Shakespeare' }, proc { " " }
# => "--Shakespeare"

proc { not_a_method }.eor proc {10 / 0 }, proc { system }
# => nil

Using the Previous Errors

proc { not_a_method }.eor proc {10 / 0 }, proc { system }, proc {|e| e}
# => #<NameError: undefined local variable or method `not_a_method' for main:Object>

proc { not_a_method }.eor proc {10 / 0 }, proc { system }, proc {|*errs| errs.last}
# => #<ArgumentError: wrong number of arguments>

proc { not_a_method }.eor proc {10 / 0 }, proc { system }, proc {|*errs| errs.inspect}
# => "[#<NameError: undefined local variable or method `not_a_method' for main:Object>,
       #<ZeroDivisionError: divided by 0>,
       #<ArgumentError: wrong number of arguments>]"

Installing and Building

To install via RubyGems (from rubygems.org)

gem install eor

To build the gem from these sources:

rake build

To install the gem from these sources:

rake install

Contact

Questions, comment, suggestions, and flames can be sent to [email protected]

Contribute

Have a patch? Email it to [email protected] or fork me and submit a pull request.

Copyright © 2010 Ben Weissmann. See LICENSE for details.