Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/benweissmann/eor

Adds #eor to Ruby's Proc class
https://github.com/benweissmann/eor

Last synced: about 17 hours ago
JSON representation

Adds #eor to Ruby's Proc class

Awesome Lists containing this project

README

        

= EOr

http://github.com/benweissmann/eor

By Ben Weissmann, mailto:[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.

== Examples

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

== 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}
# => #

proc { not_a_method }.eor proc {10 / 0 }, proc { system }, proc {|*errs| errs.last}
# => #

proc { not_a_method }.eor proc {10 / 0 }, proc { system }, proc {|*errs| errs.inspect}
# => "[#,
#,
#]"

== Installing and Building

To install via RubyGems (from http://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 mailto:[email protected]

== Contribute

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

== Copyright

Copyright (c) 2010 Ben Weissmann. See LICENSE for details.