Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Some ruby functions for those of us who still have to work with php.

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 test
Octocat-spinner-32 README.md
Octocat-spinner-32 ruby.php
README.md

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:

$myArray['someKey'];

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

$myArray->fetch('someKey');
# or
$myArray->unwrap()['someKey'];

Implemented functionality

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

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

all

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

any

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

compact

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

collect/map

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

each

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

each_with_index

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

inject/reduce

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

join

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

reject

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

select

$a->select(function($i){return $i%2==0;}); // [2]
Something went wrong with that request. Please try again.