Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/coreyhaines/enumeradical

Radical enumerable additions for enumerating and doing other awesome things
https://github.com/coreyhaines/enumeradical

Last synced: about 2 months ago
JSON representation

Radical enumerable additions for enumerating and doing other awesome things

Awesome Lists containing this project

README

        

[![Build Status](https://travis-ci.org/coreyhaines/enumeradical.svg?branch=master)](https://travis-ci.org/coreyhaines/enumeradical)
[![Code Climate](https://codeclimate.com/github/coreyhaines/enumeradical/badges/gpa.svg)](https://codeclimate.com/github/coreyhaines/enumeradical)

# Enumeradical
## A most amazing collection of useful functions filling common tasks when iterating over collections

## What is this?
I love enumerable. I really do. I use the functions it provides with the utmost alacrity. Nothing makes me sadder than seeing a #each used to populate an array. Once you start using them a lot in production systems, you notice a bunch of common patterns.

## How do I use it?
It is a gem, so just do

gem install enumeradical

then rock the house by requiring it. It sets itself up!

require 'enumeradical'

## Why would I use it?

### I have an array of objects, and I need to convert them to another type.

class MyNumberPresenter
def initialize(number)
@number = number
end
end

[1,2,3].map { |number| MyNumberPresenter.new(number) }
# => [#, #,
#]

**NO MORE!** Use Enumerable#map_to(type)

class MyNumberPresenter
def initialize(number)
@number = number
end
end

[1,2,3].map_to MyNumberPresenter
# => [#, #,
#]

### I have an array of objects, and I want to map them to the value they give from indexing into another object.

require 'date'
[1,2,3].map { |index| Date::ABBR_DAYNAMES[index] } # => ["Mon", "Tue", "Wed"]

**NO MORE!** Use Enumerable#map_into

require 'date'
[1,2,3].map_into Date::ABBR_DAYNAMES # => ["Mon", "Tue", "Wed"]

### I have an array of objects, and I'd like to convert them using a given object's method.

class Converter
def hellos(times)
"hello"*times
end
end

converter = Converter.new

[1,2,3].map { |times| converter.hellos(times) }
# => ["hello", "hellohello", "hellohellohello"]

**NO MORE!** Use Object#map_over

class Converter
def hellos(times)
"hello"*times
end
end

converter = Converter.new

converter.map_over [1,2,3], :hellos
# => ["hello", "hellohello", "hellohellohello"]

### I have an array of objects, and I'd like to sort them based on another enumerable.

class Thingy
def initialize(foo)
self.foo = foo
end
attr_accessor :foo
end

thingies = [Thingy.new("abc"), Thingy.new("def"), Thingy.new("ghi")]
arry = ["def", "abc", "ghi"]

thingies.sort_like(arry, :foo)
# OR
thingies.sort_like(arry) { |t| t.foo }

## Is this useful?
YES!!!!! Use it.

## Who built this
[Corey Haines](http://github.com/coreyhaines) and [Ryan Briones](http://github.com/ryanbriones)

## License

MIT. See [LICENSE](https://github.com/coreyhaines/enumeradical/blob/master/License.txt)