SlideShare une entreprise Scribd logo
1  sur  17
Télécharger pour lire hors ligne
Enumerables
Ginger Griffis
Tuesday, August 13, 13
Enumerable is a Module
Mixin
Tuesday, August 13, 13
What the eff is that??
Tuesday, August 13, 13
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
Tuesday, August 13, 13
So..What is a Mixin?
• A Mixin is a Module
• A SPECIAL type of
Module
Tuesday, August 13, 13
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
So, as I was saying, Enumerable
is a Module Mixin
• Provides Collection type classes with
traversal, searching, and sorting functionality
Tuesday, August 13, 13
Examples in Standard
Ruby Library
• Some of your BFFs include Enumerable
• Array, Hash, Set
• Range, File
Tuesday, August 13, 13
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
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
And NOW for
the fun part!
Tuesday, August 13, 13
Mixin Match
You can add the Enumerable
Module to your own classes!
Tuesday, August 13, 13
Tuesday, August 13, 13
Hokey Pokey!!
Tuesday, August 13, 13
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
GingerGriffis@hotmail.com
github.com/ggriffis
http://www.linkedin.com/in/gingergriffis
@GingerGriffis
Tuesday, August 13, 13

Contenu connexe

En vedette (7)

Colorado school of public health environmental and occupational health master...
Colorado school of public health environmental and occupational health master...Colorado school of public health environmental and occupational health master...
Colorado school of public health environmental and occupational health master...
 
萬用 Ghost win 7 映像檔製作教學
萬用 Ghost win 7 映像檔製作教學萬用 Ghost win 7 映像檔製作教學
萬用 Ghost win 7 映像檔製作教學
 
In-the-Bag Program_PPT_5_v3
In-the-Bag Program_PPT_5_v3In-the-Bag Program_PPT_5_v3
In-the-Bag Program_PPT_5_v3
 
Compiled_Finalbook.v.5
Compiled_Finalbook.v.5Compiled_Finalbook.v.5
Compiled_Finalbook.v.5
 
Colorado School of Public Health Environmental and Occupational Health Master...
Colorado School of Public Health Environmental and Occupational Health Master...Colorado School of Public Health Environmental and Occupational Health Master...
Colorado School of Public Health Environmental and Occupational Health Master...
 
Org development for bbm ii sem bangalore university
Org development for bbm ii sem bangalore universityOrg development for bbm ii sem bangalore university
Org development for bbm ii sem bangalore university
 
Organisational development b.com vith sem (optional) Bangalore University
Organisational development b.com vith sem (optional) Bangalore UniversityOrganisational development b.com vith sem (optional) Bangalore University
Organisational development b.com vith sem (optional) Bangalore University
 

Similaire à Enumerables (6)

Just Enough Category Theory for Haskell, part 1
Just Enough Category Theory for Haskell, part 1Just Enough Category Theory for Haskell, part 1
Just Enough Category Theory for Haskell, part 1
 
The Enumerable Module or How I Fell in Love with Ruby
The Enumerable Module or How I Fell in Love with RubyThe Enumerable Module or How I Fell in Love with Ruby
The Enumerable Module or How I Fell in Love with Ruby
 
02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt
 
CS 151 Classes lecture
CS 151 Classes lectureCS 151 Classes lecture
CS 151 Classes lecture
 
Ruby object model - Understanding of object play role for ruby
Ruby object model - Understanding of object play role for rubyRuby object model - Understanding of object play role for ruby
Ruby object model - Understanding of object play role for ruby
 
Search and Sort algorithms. Bubble, Insertion, Selection.
Search and Sort algorithms. Bubble, Insertion, Selection.Search and Sort algorithms. Bubble, Insertion, Selection.
Search and Sort algorithms. Bubble, Insertion, Selection.
 

Dernier

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Dernier (20)

Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 

Enumerables

  • 2. Enumerable is a Module Mixin Tuesday, August 13, 13
  • 3. What the eff is that?? Tuesday, August 13, 13
  • 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
  • 6. So..What is a Mixin? • A Mixin is a Module • A SPECIAL type of Module Tuesday, August 13, 13
  • 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. 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. Examples in Standard Ruby Library • Some of your BFFs include Enumerable • Array, Hash, Set • Range, File Tuesday, August 13, 13
  • 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. 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. And NOW for the fun part! Tuesday, August 13, 13
  • 13. Mixin Match You can add the Enumerable Module to your own classes! Tuesday, August 13, 13
  • 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