Enumerables

334 views

Published on

Enumerables Lightning Talk Codefellows RoR Bootcamp
Sample code: https://github.com/ggriffis/cat-hokey-pokey
YouTube of presentation: http://www.youtube.com/watch?v=bcVAl3tLntc

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
334
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
3
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Enumerables

  1. 1. Enumerables Ginger Griffis Tuesday, August 13, 13
  2. 2. Enumerable is a Module Mixin Tuesday, August 13, 13
  3. 3. What the eff is that?? Tuesday, August 13, 13
  4. 4. What is a Module? • A Module is a collection of methods and constants. • Similar to a regular Class but without the concept of instantiation • Used as a standalone entity, it can help with namespacing Tuesday, August 13, 13
  5. 5. Tuesday, August 13, 13
  6. 6. So..What is a Mixin? • A Mixin is a Module • A SPECIAL type of Module Tuesday, August 13, 13
  7. 7. Special How? • A Mixin happens when you include a Module within a Class • Instance Methods come to life! • Allows a sense of multiple inheritance • 2 objects marry and the result is greater than the sum of the parts Tuesday, August 13, 13
  8. 8. So, as I was saying, Enumerable is a Module Mixin • Provides Collection type classes with traversal, searching, and sorting functionality Tuesday, August 13, 13
  9. 9. Examples in Standard Ruby Library • Some of your BFFs include Enumerable • Array, Hash, Set • Range, File Tuesday, August 13, 13
  10. 10. Some Cool Enumerable Methods • all? (do all items return true for the block) • any? (do any items return true for the block) • collect (make new collection with results) • count • detect (return the first item that returns true for the block) • find • first • grep (return all items that === a pattern) • inject (iterates through the collection with an accumulator) • reject (returns all items that return false for the block) • select (returns all items that return true for the block) Tuesday, August 13, 13
  11. 11. Methods that must be defined if a class includes Enumerable • each (basically some flavor of yielding to a block for each element in your object) • If the class wishes to use the max, min, or sort functions provided by Enumerable • The “each” items of the collection must implement <=> • Combined comparison operator. Returns 0 if first operand equals second, 1 if first operand is greater than the second and -1 if first operand is less than the second. Tuesday, August 13, 13
  12. 12. And NOW for the fun part! Tuesday, August 13, 13
  13. 13. Mixin Match You can add the Enumerable Module to your own classes! Tuesday, August 13, 13
  14. 14. Tuesday, August 13, 13
  15. 15. Hokey Pokey!! Tuesday, August 13, 13
  16. 16. Each to her Own • http://ruby-doc.org/core-2.0/ Enumerable.html • http://www.ruby-doc.org/docs/ ProgrammingRuby/html/tut_modules.html Tuesday, August 13, 13
  17. 17. GingerGriffis@hotmail.com github.com/ggriffis http://www.linkedin.com/in/gingergriffis @GingerGriffis Tuesday, August 13, 13

×