SlideShare a Scribd company logo
Inside Enumerable Mike Bowler Gargoyle Software Inc.
Agenda What is Enumerable? What can you do with it? How does it do that?
What is it? It’s a group of related methods all having to do with collections It’s the interesting part behind Array, Set, Hash and Range It’s a Module (often called a mixin)
hash = { :a  => 1,  :b  => 2} hash.collect  do  |key, value|  value * 4 end [4,8] [4,8,12] [4,8,12] array = [1,2,3] array.collect  do  |value| value * 4 end range = 1..3 range.collect  do  |value| value * 4 end
hash = { :a  => 1,  :b  => 2} hash.collect  do  |key, value|  value * 4 end array = [1,2,3] array.collect  do  |value| value * 4 end range = 1..3 range.collect  do  |value| value * 4 end module  Enumerable def  collect ... end end
multiple inheritance
mixins
Mixins Cannot be instantiated Can be mixed in
Mixins usually  don’t stand alone don’t stand alone Typically... mixins aren’t completely self-contained  they rely on certain methods being present on the class they are mixed into This isn’t mandatory but is very common Let’s look at an example...
Enumerable requires each() [1,2,3].collect {|i| i*2} [2,4,6]
Enumerable.collect class  Foo include  Enumerable def  each yield 1 yield 2 yield 3 end end Foo.new.collect do |num| num * 2 end module  Enumerable def  collect array = [] each do |a| array <<  yield (a) end array end end
Enumerable.collect module  Enumerable def  collect array = [] each do |a| array <<  yield (a) end array end end class  Foo include  Enumerable def  each yield 1 yield 2 yield 3 end end Foo.new.collect do |num| num * 2 end
Enumerable.collect module  Enumerable def  collect array = [] each do |a| array <<  yield (a) end array end end class  Foo include  Enumerable def  each yield 1 yield 2 yield 3 end end Foo.new.collect do |num| num * 2 end
Enumerable.collect class  Foo include  Enumerable def  each yield 1 yield 2 yield 3 end end Foo.new.collect do |num| num * 2 end module  Enumerable def  collect array = [] each do |a| array <<  yield (a) end array end end
Enumerable.collect module  Enumerable def  collect array = [] each do |a| array <<  yield (a) end array end end class  Foo include  Enumerable def  each yield 1 yield 2 yield 3 end end Foo.new.collect do |num| num * 2 end
Enumerable.collect class  Foo include  Enumerable def  each yield 1 yield 2 yield 3 end end Foo.new.collect do |num| num * 2 end module  Enumerable def  collect array = [] each do |a| array <<  yield (a) end array end end Result [2,4,6]
Required methods collect() would not have worked if Foo hadn’t declared each() All methods in Enumerable require each() A couple also require <=> min(), max(), sort() Comparison operator
Enumerable methods all?, any?, collect, detect, each_cons, each_slice, each_with_index, entries, enum_cons, enum_slice, enum_with_index, find, find_all, grep, include?, inject, map, max, member?, min, partition, reject, select, sort, sort_by, to_a, to_set, zip
any? [1,2,3,4].any? {|a| a > 2} Returns true if this block... ...returns true for  any of these values
all? [1,2,3,4].all? {|a| a > 2} Returns true if this block... ...returns true for  ALL of these values
collect, map “Returns a new array with the results of running block once for every element in enum” map is an alias for collect [1,2,3].collect {|i| i*2} [2,4,6]
find, detect find(ifnone = nil) {| obj | block } => obj or nil Passes each entry in enum to block. Returns the first for which block is not false. If no object matches, calls ifnone and returns its result when it is specified, or returns nil detect is an alias for find
find_all, select find_all {| obj | block } => array Returns an array containing all elements of enum for which block is not false select is an alias for find_all
grep grep(pattern) => array Returns an array of every element in enum for which Pattern === element. If the optional block is supplied, each matching element is passed to it, and the block‘s result is stored in the output array Case equality
Case equality For most objects, === is the same as == Case equality Regular equality
Case equality case myvar when 1..50 # do something when 51 # do something when /^\d+$/ # do something end Within the range This exact number Matches this regular expression Different kind of equality as a convenience in case statements
Case equality in grep [1,2,3].grep(1..50) [1,2,3].grep(51) [1,2,3].grep(/^\d+$/) Within the range This exact number Matches this regular expression
Wrap up Enumerable is already mixed into all the core collections You can mix it into any of your classes so long as you implement each() It provides all kinds of useful methods to walk across the collections and get information out of them
Shameless Plug I can help you with your Ruby and/or Rails projects.  Ask me how. Mike Bowler [email_address] www.GargoyleSoftware.com (company) www.SphericalImprovement.com (blog)

More Related Content

PDF
Arrays in python
PDF
Introduction to python programming
PDF
Python Workshop Part 2. LUG Maniapl
PPTX
List in Python
PDF
Data type list_methods_in_python
PDF
A Gentle Introduction to Functional Paradigms in Ruby
PPTX
Python programming for Beginners - II
PDF
Arrays In Python | Python Array Operations | Edureka
Arrays in python
Introduction to python programming
Python Workshop Part 2. LUG Maniapl
List in Python
Data type list_methods_in_python
A Gentle Introduction to Functional Paradigms in Ruby
Python programming for Beginners - II
Arrays In Python | Python Array Operations | Edureka

What's hot (20)

PDF
List,tuple,dictionary
PPTX
2.4 multiplication of real numbers 1
PDF
List , tuples, dictionaries and regular expressions in python
PPTX
C# Arrays
PPTX
Python for Beginners(v3)
ODP
Python quickstart for programmers: Python Kung Fu
PPTX
Introduction to python programming 2
PPS
Higher Order Procedures (in Ruby)
PDF
Python programming : Arrays
PPTX
Introduction to python programming 1
PPTX
PPTX
Numerical analysisgroup19
PPTX
Python PCEP Loops
PPTX
Python programming workshop
PPTX
PDF
Introduction to python
PDF
Slicing in Python - What is It?
ODP
List,tuple,dictionary
2.4 multiplication of real numbers 1
List , tuples, dictionaries and regular expressions in python
C# Arrays
Python for Beginners(v3)
Python quickstart for programmers: Python Kung Fu
Introduction to python programming 2
Higher Order Procedures (in Ruby)
Python programming : Arrays
Introduction to python programming 1
Numerical analysisgroup19
Python PCEP Loops
Python programming workshop
Introduction to python
Slicing in Python - What is It?
Ad

Similar to Inside Enumerable (20)

PPTX
Ruby Enumerable
PDF
Enumerables
KEY
Enumerables
PDF
Ruby Cold Cuts Part 1
PPT
Enumerable
PDF
Let’s Talk About Ruby
PDF
The Enumerable Module or How I Fell in Love with Ruby
PPTX
Ruby data types and objects
KEY
Functional ruby
PDF
Elixir & Phoenix – fast, concurrent and explicit
PDF
Thinking Functionally In Ruby
PDF
19 ruby iterators
PDF
Ruby — An introduction
PDF
Ruby Topic Maps Tutorial (2007-10-10)
PPT
Ruby For Java Programmers
PDF
Elixir & Phoenix – fast, concurrent and explicit
PDF
Ruby 1.9
PDF
Ruby presentasjon på NTNU 22 april 2009
PDF
Ruby presentasjon på NTNU 22 april 2009
PDF
Ruby presentasjon på NTNU 22 april 2009
Ruby Enumerable
Enumerables
Enumerables
Ruby Cold Cuts Part 1
Enumerable
Let’s Talk About Ruby
The Enumerable Module or How I Fell in Love with Ruby
Ruby data types and objects
Functional ruby
Elixir & Phoenix – fast, concurrent and explicit
Thinking Functionally In Ruby
19 ruby iterators
Ruby — An introduction
Ruby Topic Maps Tutorial (2007-10-10)
Ruby For Java Programmers
Elixir & Phoenix – fast, concurrent and explicit
Ruby 1.9
Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009
Ad

More from Mike Bowler (7)

PDF
Retrospective Magic - Toronto Agile Conference
PDF
Retrospective science
PDF
Brain Talk: More effective conversations through clean language
PDF
Continuous Delivery: Responding to Change Faster Than Ever Before - SDEC14
PDF
Continuous Delivery for Agile Teams
PDF
Exploring the magic behind dynamic finders: diving into ActiveRecord::Base.me...
PDF
Date Once
Retrospective Magic - Toronto Agile Conference
Retrospective science
Brain Talk: More effective conversations through clean language
Continuous Delivery: Responding to Change Faster Than Ever Before - SDEC14
Continuous Delivery for Agile Teams
Exploring the magic behind dynamic finders: diving into ActiveRecord::Base.me...
Date Once

Recently uploaded (20)

PDF
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PDF
madgavkar20181017ppt McKinsey Presentation.pdf
PDF
Top Generative AI Tools for Patent Drafting in 2025.pdf
PPTX
ABU RAUP TUGAS TIK kelas 8 hjhgjhgg.pptx
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
PDF
Dell Pro 14 Plus: Be better prepared for what’s coming
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Sensors and Actuators in IoT Systems using pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
DevOps & Developer Experience Summer BBQ
PDF
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
PDF
Transforming Manufacturing operations through Intelligent Integrations
PDF
Google’s NotebookLM Unveils Video Overviews
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
KodekX | Application Modernization Development
PDF
A Day in the Life of Location Data - Turning Where into How.pdf
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
GamePlan Trading System Review: Professional Trader's Honest Take
madgavkar20181017ppt McKinsey Presentation.pdf
Top Generative AI Tools for Patent Drafting in 2025.pdf
ABU RAUP TUGAS TIK kelas 8 hjhgjhgg.pptx
CIFDAQ's Market Insight: SEC Turns Pro Crypto
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
Dell Pro 14 Plus: Be better prepared for what’s coming
NewMind AI Weekly Chronicles - August'25 Week I
Sensors and Actuators in IoT Systems using pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
DevOps & Developer Experience Summer BBQ
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
Transforming Manufacturing operations through Intelligent Integrations
Google’s NotebookLM Unveils Video Overviews
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
KodekX | Application Modernization Development
A Day in the Life of Location Data - Turning Where into How.pdf

Inside Enumerable

  • 1. Inside Enumerable Mike Bowler Gargoyle Software Inc.
  • 2. Agenda What is Enumerable? What can you do with it? How does it do that?
  • 3. What is it? It’s a group of related methods all having to do with collections It’s the interesting part behind Array, Set, Hash and Range It’s a Module (often called a mixin)
  • 4. hash = { :a => 1, :b => 2} hash.collect do |key, value| value * 4 end [4,8] [4,8,12] [4,8,12] array = [1,2,3] array.collect do |value| value * 4 end range = 1..3 range.collect do |value| value * 4 end
  • 5. hash = { :a => 1, :b => 2} hash.collect do |key, value| value * 4 end array = [1,2,3] array.collect do |value| value * 4 end range = 1..3 range.collect do |value| value * 4 end module Enumerable def collect ... end end
  • 8. Mixins Cannot be instantiated Can be mixed in
  • 9. Mixins usually don’t stand alone don’t stand alone Typically... mixins aren’t completely self-contained they rely on certain methods being present on the class they are mixed into This isn’t mandatory but is very common Let’s look at an example...
  • 10. Enumerable requires each() [1,2,3].collect {|i| i*2} [2,4,6]
  • 11. Enumerable.collect class Foo include Enumerable def each yield 1 yield 2 yield 3 end end Foo.new.collect do |num| num * 2 end module Enumerable def collect array = [] each do |a| array << yield (a) end array end end
  • 12. Enumerable.collect module Enumerable def collect array = [] each do |a| array << yield (a) end array end end class Foo include Enumerable def each yield 1 yield 2 yield 3 end end Foo.new.collect do |num| num * 2 end
  • 13. Enumerable.collect module Enumerable def collect array = [] each do |a| array << yield (a) end array end end class Foo include Enumerable def each yield 1 yield 2 yield 3 end end Foo.new.collect do |num| num * 2 end
  • 14. Enumerable.collect class Foo include Enumerable def each yield 1 yield 2 yield 3 end end Foo.new.collect do |num| num * 2 end module Enumerable def collect array = [] each do |a| array << yield (a) end array end end
  • 15. Enumerable.collect module Enumerable def collect array = [] each do |a| array << yield (a) end array end end class Foo include Enumerable def each yield 1 yield 2 yield 3 end end Foo.new.collect do |num| num * 2 end
  • 16. Enumerable.collect class Foo include Enumerable def each yield 1 yield 2 yield 3 end end Foo.new.collect do |num| num * 2 end module Enumerable def collect array = [] each do |a| array << yield (a) end array end end Result [2,4,6]
  • 17. Required methods collect() would not have worked if Foo hadn’t declared each() All methods in Enumerable require each() A couple also require <=> min(), max(), sort() Comparison operator
  • 18. Enumerable methods all?, any?, collect, detect, each_cons, each_slice, each_with_index, entries, enum_cons, enum_slice, enum_with_index, find, find_all, grep, include?, inject, map, max, member?, min, partition, reject, select, sort, sort_by, to_a, to_set, zip
  • 19. any? [1,2,3,4].any? {|a| a > 2} Returns true if this block... ...returns true for any of these values
  • 20. all? [1,2,3,4].all? {|a| a > 2} Returns true if this block... ...returns true for ALL of these values
  • 21. collect, map “Returns a new array with the results of running block once for every element in enum” map is an alias for collect [1,2,3].collect {|i| i*2} [2,4,6]
  • 22. find, detect find(ifnone = nil) {| obj | block } => obj or nil Passes each entry in enum to block. Returns the first for which block is not false. If no object matches, calls ifnone and returns its result when it is specified, or returns nil detect is an alias for find
  • 23. find_all, select find_all {| obj | block } => array Returns an array containing all elements of enum for which block is not false select is an alias for find_all
  • 24. grep grep(pattern) => array Returns an array of every element in enum for which Pattern === element. If the optional block is supplied, each matching element is passed to it, and the block‘s result is stored in the output array Case equality
  • 25. Case equality For most objects, === is the same as == Case equality Regular equality
  • 26. Case equality case myvar when 1..50 # do something when 51 # do something when /^\d+$/ # do something end Within the range This exact number Matches this regular expression Different kind of equality as a convenience in case statements
  • 27. Case equality in grep [1,2,3].grep(1..50) [1,2,3].grep(51) [1,2,3].grep(/^\d+$/) Within the range This exact number Matches this regular expression
  • 28. Wrap up Enumerable is already mixed into all the core collections You can mix it into any of your classes so long as you implement each() It provides all kinds of useful methods to walk across the collections and get information out of them
  • 29. Shameless Plug I can help you with your Ruby and/or Rails projects. Ask me how. Mike Bowler [email_address] www.GargoyleSoftware.com (company) www.SphericalImprovement.com (blog)