https://github.com/rossta/loves-enumerable
Code samples for a presentation on the joy of Ruby Enumerable
https://github.com/rossta/loves-enumerable
Last synced: about 1 year ago
JSON representation
Code samples for a presentation on the joy of Ruby Enumerable
- Host: GitHub
- URL: https://github.com/rossta/loves-enumerable
- Owner: rossta
- Created: 2015-11-08T18:52:26.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2016-04-14T15:43:56.000Z (about 10 years ago)
- Last Synced: 2025-04-26T06:18:18.625Z (about 1 year ago)
- Language: Ruby
- Size: 5.45 MB
- Stars: 45
- Watchers: 3
- Forks: 14
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Enumerable: How I fell in love with Ruby
This repository contains code snippets used in my presentation on Ruby's enumerable module. Ruby code was written to run on Ruby 2.2.
[Enumerable presentation on Slidedeck](https://speakerdeck.com/rossta/enumerable-how-i-fell-in-love-with-ruby)
## Usage
To run a sample, clone the repo and install the gems:
```bash
$ git clone https://github.com/rossta/loves-enumerable.git
$ cd loves-enumerable
$ bundle install
```
Then simply run the code with the ruby executable.
```bash
$ ruby code/pascals_triangle.rb
```
You'll get the most out of this repo by inspecting the source along with following the presentation.
## Main Points
Use more of the Enumerable API
- look for more direct solutions
- read the docs
Learn how Enumerable methods are implemented
- it’s not magic
- implement Enumerable on your own
- create your own extensions
Encapsulate complexity of enumerating
- e.g. stream data to client,
- deferred data fetching
Provide Enumerators
- for any method that returns an enumerable with a block:
return an enumerator when the block is missing
When to be Lazy
- for avoiding eager evaluation
- for filtering results over large collection
Embrace the functional flavor
- given the same inputs, you can expect the same results
- use Enumerables like Legos: as building blocks
## Resources
Many of the ideas and examples are adapted from other great sources. Please
check out the following to learn more.
#### General
* https://practicingruby.com/articles/building-enumerable-and-enumerator
* http://www.sitepoint.com/guide-ruby-collections-iii-enumerable-enumerator/
* http://jwb.io/20130114-building-sequences-with-enumerator.html
* https://speakerdeck.com/jeg2/10-things-you-didnt-know-ruby-could-do
* http://dgiim.github.io/blog/2015/08/24/ruby-enumerators/
#### Lazy Enumerators
* http://patshaughnessy.net/2013/4/3/ruby-2-0-works-hard-so-you-can-be-lazy
* http://railsware.com/blog/2012/03/13/ruby-2-0-enumerablelazy/
* http://blog.honeybadger.io/using-lazy-enumerators-to-work-with-large-files-in-ruby/
* https://gist.github.com/barmstrong/1323865 # processing large csv
* http://www.sitepoint.com/implementing-lazy-enumerables-in-ruby/
#### Streaming
* http://old.blog.phusion.nl/2014/08/03/why-rails-4-live-streaming-is-a-big-deal
* http://www.sitepoint.com/streaming-with-rails-4/
* http://smsohan.com/blog/2013/05/09/genereating-and-streaming-potentially-large-csv-files-using-ruby-on-rails
#### Code
* https://github.com/rubinius/rubinius/blob/master/kernel/common/enumerable.rb
* https://github.com/rubinius/rubinius/blob/master/kernel/common/enumerator.rb
* https://github.com/sferik/twitter/blob/master/lib/twitter/enumerable.rb
* https://github.com/bbatsov/powerpack/tree/master/lib/powerpack/enumerable
* https://github.com/rubyworks/facets
#### Data sources
* http://catalog.data.gov/dataset/college-scorecard
* http://www.gutenberg.org
#### Books
* The Well-Grounded Rubyist by David Black