AS#Enumerable
AS#Enumerable is a port of the Ruby Enumerable API for Actionscript 2.0. It provides the following enumerable methods for use in your flash applications:
- all,
- any,
- collect,
- detect,
- each,
- find,
- findAll,
- include,
- inject,
- map,
- max,
- member,
- min,
- pluck,
- reject,
- select,
- size,
- sortBy
It's provided as a subclass of the native Array object, but if you want all native Arrays to inherit the enumerable interface (a la Prototype) just add:
Array.prototype = new Enumerable();to your Flash application. Some basic sample uses of Enumerable:
var enum = [];
enum.push(0.2);
enum.push(0.3);
enum.each(function(item,index) {
trace(item + ':' + index);
});
(Using each for basic iteration).
var enum = [];
enum.push({value:'foo'});
enum.push({value:'bar'});
enum.push({value:'camp'});
var item = enum.find(function(item,index) {
return item.value == 'bar';
});
(Using find to fetch an object from a collection).
var enum = [];
enum.push('foo');
enum.push('bar');
enum.push('camp');
trace(Math.floor(enum.inject(0,function(strlen,string) {
return strlen + string.length;
})/enum.size()));
(Using inject and size to fetch the average string length in a collection of strings).
Go ahead and grab the source code, which will find its permanent home at /code, should you want to check back.
See the prototype enumerable API doc for further documentation, as the usage examples are identical in Actionscript 2.0. Thanks to Sam Stephenson of Prototype for the JS port.





