Skip to content

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

Notifications You must be signed in to change notification settings

finbarr/rubyphp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 

Repository files navigation

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]

About

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

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages