Some ruby functions for those of us who still have to work with php.
Latest commit cc17439 Dec 20, 2013 @finbarr Merge pull request #2 from zizkovrb/master
A little love made to
Failed to load latest commit information.
test Fleshed out the README file and minor tweaks Dec 3, 2011 A little love made to Dec 8, 2013
ruby.php adding more functions Dec 3, 2011

Ruby functions for PHP

After working with Ruby full time for over a month, I went back to a PHP project and decided something was missing...

This project aims to port some of the lovely ruby methods for working with arrays and hashes to PHP.

Sneak preview

$myArray = Ruby::wrap(array(1,2,3));
$sum = $myArray->inject(0, function($sum,$i){return $sum + $i});
echo $sum; // prints 6

There are implementations for many of the common methods - scroll down to see examples.

Using the code

There are two ways to use the code: you can treat it as a set of helper methods or as an array wrapper.

Helper method style

Ruby::method($myArray, function($args){ #evaluate });

Array wrapper style

$a = Ruby::collection();
# or
$a = Ruby::wrap($myArray);
# both
$a->method(function($args){ #evaluate });

Which should I use?

The Helper method style is a little more verbose but maintains your direct access to the array:


The Array wrapper style is neater but more verbose for array access:

# or

Implemented functionality

The following examples assume a wrapped php array containing the values 1,2,3

$a = Ruby::wrap(array(1,2,3));


$a->all(function($i){return is_number($i)}); // TRUE


$a->any(function($i){return $i==1;}); // TRUE


$a->push(NULL); // [1,2,3,NULL]
$a->compact(); // [1,2,3]


$a->collect(function($i){return $i*2;}); // [2,4,6]


$a->each(function($i){echo $i;}); // prints 1 2 3


$a->each_with_index(function($i,$d){echo $i + $d;}) // prints 1 3 5


$a->inject(0, function($sum,$i){return $sum + $i;}); // 6


$a->join(','); // '1,2,3'


$a->reject(function($i){return $i%2==0;}); // [1,3]


$a->select(function($i){return $i%2==0;}); // [2]