By Ben Weissmann, ben@benweissmann.com
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.
The original use case was parsing dates. I had some data about start and end times that had nicely formatted dates that Date.parse() could handle, such as “2010-09-01 12:12:12”. However, if the date didn’t exist (for example, something that’s still going on wouldn’t have an end date), the data would be all zeros, like “0000-00-00 00:00:00”. Date.parse would choke on this and throw an error. So I had to do:
begin date = Date.parse(string) rescue date = nil end
with eor, I can do:
proc{ Date.parse(string) }.eor
And if I had wanted to use the current date as a backup, I could have done
proc{ Date.parse(string) }.eor Date.new
You can use it for multiple libraries that provide the same functions. For example, hpricot and nokogiri are both XML libraries. If my code needs either one (but I don’t care which), I can use
proc{ require 'hpricot' }.eor { require 'nokogiri' }
That way, if the user has hpricot, it’ll use that, and if not, it’ll use nokogiri.
You can can also use it for easy error handling. Let’s say you’re writing something like irb, and you want to take some string of Ruby code and either display the result of that code, or the error it generated. You could do
result = proc{ eval(string) }.eor {|error| error} puts result
See the spec folder for more in-depth examples.
proc{ 'brevity' }.eor # => "brevity" proc { not_a_method }.eor # => nil
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
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
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>]"
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
Questions, comment, suggestions, and flames can be sent to ben@benweissmann.com
Have a patch? Email it to ben@benweissmann.com or fork me and submit a pull request.
Copyright © 2010 Ben Weissmann. See LICENSE for details.