Gonzo Array Extensions

Extends the Ruby core Array class, and augments some of its methods with additional functionality.

Install

gem install gonzo_array_extensions --source http://gemcutter.org

Usage

Gonzo Array Extensions augments the Array methods first, last and include? so you can pass an optional block to each so you can check for additional information.

To use the library just include the gem in your code…

include 'rubygems'
include 'gonzo_array_extensions'

Array#first

As well as supporting the original functionality of Array#first you can also pass an additional block and the first element(s) matching the block will be returned.

a = [ 1, 2, 3, 4, 5, 6 ]
a.first { |x| x > 3 }    #=> 4
a.first(1) { |x| x > 3 } #=> [4]
a.first(3) { |x| x > 3 } #=> [4, 5, 6]
a.first { |x| x > 6 }    #=> nil

Array#last

As well as supporting the original functionality of Array#last you can also pass an additional block and the last element(s) matching the block will be returned.

a = [ 1, 2, 3, 4, 5, 6 ]
a.last { |x| x < 3 }    #=> 2
a.last(1) { |x| x < 3 } #=> [2]
a.last(3) { |x| x < 5 } #=> [2, 3, 4]  
a.first { |x| x > 6 }   #=> nil

Array#include?

As well as supporting the original functionality of Array#include? you can also pass an additional block and the method will return true if any element(s) match.

a = [ 1, 2, 3, 4, 5, 6 ]
a.include? { |x| x < 3 }    #=> true
a.include? { |x| x > 6 }    #=> false

Thanks

Thats it really!

Love Rob.

License

Copyright © 2010 Robert Oles

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.