Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/coreyhaines/enumeradical
- Owner: coreyhaines
- License: mit
- Created: 2012-02-14T21:45:30.000Z (over 12 years ago)
- Default Branch: main
- Last Pushed: 2021-02-14T15:17:54.000Z (over 3 years ago)
- Last Synced: 2024-05-11T20:22:03.887Z (5 months ago)
- Language: Ruby
- Homepage:
- Size: 19.5 KB
- Stars: 26
- Watchers: 3
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.markdown
- License: License.txt
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 dogem 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
endconverter = 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
endconverter = 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
endthingies = [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)