enumerables

17
Enumerables Ginger Griffis Tuesday, August 13, 13

Upload: ginger-griffis

Post on 15-May-2015

219 views

Category:

Technology


0 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Enumerables

Enumerables Ginger Griffis

Tuesday, August 13, 13

Page 2: Enumerables

Enumerable is a Module Mixin

Tuesday, August 13, 13

Page 3: Enumerables

What the eff is that??

Tuesday, August 13, 13

Page 4: Enumerables

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

Page 5: Enumerables

Tuesday, August 13, 13

Page 6: Enumerables

So..What is a Mixin?

• A Mixin is a Module

• A SPECIAL type of Module

Tuesday, August 13, 13

Page 7: Enumerables

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

Page 8: Enumerables

So, as I was saying, Enumerable is a Module Mixin

• Provides Collection type classes with traversal, searching, and sorting functionality

Tuesday, August 13, 13

Page 9: Enumerables

Examples in Standard Ruby Library

• Some of your BFFs include Enumerable

• Array, Hash, Set

• Range, File

Tuesday, August 13, 13

Page 10: Enumerables

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

Page 11: Enumerables

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

Page 12: Enumerables

And NOW for the fun part!

Tuesday, August 13, 13

Page 13: Enumerables

Mixin MatchYou can add the Enumerable Module to your own classes!

Tuesday, August 13, 13

Page 14: Enumerables

Tuesday, August 13, 13

Page 15: Enumerables

Hokey Pokey!!

Tuesday, August 13, 13

Page 17: Enumerables

[email protected]

github.com/ggriffis

http://www.linkedin.com/in/gingergriffis

@GingerGriffis

Tuesday, August 13, 13