SlideShare uma empresa Scribd logo
1 de 41
Baixar para ler offline
THE ENUMERABLE MODULE
                         or How I Fell In Love with Ruby!


                                  Haris Amin
                          Cascadia Ruby Conf 2011
                                  07/29/2011



Monday, August 1, 2011
WHO IS THIS GUY?


    • Haris Amin

    • Software/Web           Developer

    • Live         in New York City




Monday, August 1, 2011
THESE GUYS MAKE SURE I’M
         NOT LIVING IN THE STREETS




Monday, August 1, 2011
WHAT WE DO @
           ?
                               Shoulder Pressing Colleagues




          Healthy Programmer
              Vertebrates


Monday, August 1, 2011
LOOK MA I HAS DEGREE!


    • Studies            Physics/Math in Undergrad

    •E       = mc^2 , doesn’t pay for food

    • Programming, for            me in college was just a means to compute
        something



Monday, August 1, 2011
LAST TALK I GAVE AT A
                            CONFERENCE?

    • Simulation             of Viscoelastic Fluids

    • What               did I do?

    • Employed   a weighted-norm least-squares finite element
        method to approximate the solution to Oldroyd-B equations

    • So...yeah... for          me programming was just a way to compute
        stuff :)


Monday, August 1, 2011
THOUGHT TO MYSELF...

                         Arrays & Hashes

                          ARE FUN!!!!



Monday, August 1, 2011
WHAT IS THE ENUMERABLE
                   MODULE?

    •A        module, you can MIX IT IN!

    •A        bunch of methods that work with collections

    • Empowers    the most notably the Array and Hash classes
        (among others i.e. Set, Range, File, etc.)




Monday, August 1, 2011
HOW TO ‘MIX-IN’ THE
                           ENUMERABLE?


    •A    class ‘including’ or ‘mixing-in’ Enumerable must define the
        ‘#each’ method

    • Yielded    items from the #each method empower the
        collection awareness for the class




Monday, August 1, 2011
class PlanetExpress
                           include Enumerable

                           def each
                             yield “Bender”
                             yield “Frye”
                             yield “Leela”
                             yield “Zoidberg”
                           end
                         end

                         PlanetExpress.new.collect do |member|
                           “#{member} works at Planet Express”
                         end



          • The    #collect method executes the provided block
              to all of the values yielded by #each

Monday, August 1, 2011
BUT WHY IS ENUMERABLE
             SO...
                           ...
                           ...
                           ...
                         SEXY!?

Monday, August 1, 2011
LOOK AT ALL THESE
                            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



Monday, August 1, 2011
PROGRAMMER
                           HAPPINESS



“Programmers often feel joy when they can concentrate on
 the creative side of programming, so Ruby is designed to
                 make programmers happy.”
                - Yukihiro Matsumoto (Matz)

Monday, August 1, 2011
WATCH OUT FOR DR.
                          ZOIDBERG’S TIPS




Monday, August 1, 2011
LET’S TAKE A LOOK AT SOME
        ENUMERABLE METHODS...



Monday, August 1, 2011
each

             names = %w{ Frye Leela Zoidberg }

             names.each do |name|
               “#{name} works at Planet Express”
             end



            • The   #each method yields items to a supplied block of
                code one at a time

            • Classes    implement #each differently




Monday, August 1, 2011
find


             names = %w{ Frye Leela Zoidberg }

             names.find { |name| name.length > 4}




            • Elegantly  simple, find one item that matches the
                condition supplied by the block

            • Consider  how a library like ActiveRecord would
                reimplemnt find from Enumerable?


Monday, August 1, 2011
group_by

             names = %w{ Frye Bender Leela Zoidberg }

             names.group_by { |name| name.length}
             # => {4=>["Frye"], 6=>["Bender"], 5=>["Leela"], 8=>["Zoidberg"]}




           • It  takes a block, and returns a hash, with the returned
               value from the block set as the key

           • Consider  how one could use this as word count for a
               document/text



Monday, August 1, 2011
grep

             names = %w{ Frye Bender Leela Zoidberg }

             names.grep(/oidber/)
             # => ["Zoidberg"]




           • Searches      for members of the collection according to a
               pattern.... pattern matching

           • It     uses the === operator for pattern matching




Monday, August 1, 2011
GREP-ALICOUS!
             stuff = [ “Zoidberg”, Pizza.new, :homeless, “Dr."]
             stuff.grep(String)
             # => [ “Zoidberg”, “Dr.”]

              • Using     the === allows us to do some fancy matching

              • We       can grep for types or objects

              • Equivalent      to stuff.select { |element| String === element}



                              Dr. Zoidberg’s Tip (The doctor is in!)
Monday, August 1, 2011
map / collection

             names = %w{ Frye Bender Leela Zoidberg }

             names.map { |name| name.downcase }
             # => [“frye”, “bender”, “leela”, “zoidberg” ]


           • Think   of it as a transformation method, that applies the
               block as a transformation

           • Always      returns a new Array with the transformation
               applied

           • Different    then #each, return value matters with #map


Monday, August 1, 2011
THE DELICIOUS ENUMERATOR
                         ... enum ... enum ... enum




Monday, August 1, 2011
ENUMERATOR
    • We   can create an Enumerator without mixing-in the
        Enumerable module and still have the power of Enumerable
        methods

    •3       ways to create Enumerator without mixing-in Enumerable

                1. Create Enumerator explicitly with a code block
                2. Attach an Enumerator to another object
               3. Create Enumerator implicitly with blockless iterators

Monday, August 1, 2011
Create Enumerator explicitly with a code block

                            e =   Enumerator.new do |y|
                              y   << “Frye”
                              y   << “Bender”
                              y   << “Leela”
                              y   << “Zoidberg”
                            end


        •y      is the yielder, an instance of Enumerator::Yielder

        • You    don’t yield from the block, you only append to the
            yielder

Monday, August 1, 2011
Attach an Enumerator to another object


                         names = %w { Frye Bender Leela Zoidberg }
                         e = names.enum_for(:select)



       • knows/learns         how to implement #each from another object

       • we’re  binding the Enumerator to the #select method of the
           names array




Monday, August 1, 2011
Create Enumerator implicitly with blockless iterators
           names = %w { Frye Leela Bender }

           names.enum_for(:select)
           # => #<Enumerator: ["Frye", "Leela", "Bender"]:map>

           names.map
           # => #<Enumerator: ["Frye", "Leela", "Bender"]:map>

       • most  iterators when called without a block return an
           Enumerator

       • our  blockless iterator returned the same Enumerator as the
           enum_for approach
Monday, August 1, 2011
BUT WHY DO WE CARE?

                         ...USES?




Monday, August 1, 2011
Add Enumerability to an existing object
                         module PlanetExpress
                            class Ship
                              PARTS= %w{ sprockets black-matter }
                              def survey_parts
                                PARTS.each {|part| yield part }
                              end
                            end
                          end

                         ship = PlanetExpress::Ship.new
                         enum = ship.enum_for(:survey_parts)


        • Now            we can use Enumerable methods on our ship object
Monday, August 1, 2011
Fine Grained Iteration

                         scenes = %w{ credits opening climax end }
                         e = scenes
                         puts e.next
                         puts e.next
                         e.rewind puts e.next

          • An           Enumerator is an object, it can maintain state

          • Think          film reels, state machines, etc...




Monday, August 1, 2011
CHAINING ENUMERATORS...
                         ...HMMM



Monday, August 1, 2011
CHAINING ENUMERATORS


    • Normally           chaining enumerators isn’t very useful

    • names.map.select           might as well be names.select

    • Most    enumerators are just passing the array of values down
        the chain



Monday, August 1, 2011
LAZY SLICE
             names = %w { Frye Bender Leela Zoidberg }
             names.each_slice(2).map do |first, second|
               “#{first} gets a slice & #{second} gets a slice”
             end

              • Instead   of creating a 2-element slices for the whole
                  array in memory, the enumerator can create slices in a
                  “lazy” manner and only create them as they are
                  needed



                          Dr. Zoidberg’s Tip (can i have a slice)
Monday, August 1, 2011
MAP WITH INDEX...WTF?
             names = %w { Leela Bender Frye Zoidberg }
             names.map.with_index do |name, i|
               “#{name} has a rank #{i}”
             end

              • There       is no #map_with_index defined in Enumerable

              • Ah       but we can chain... chain... chain!




                              Dr. Zoidberg’s Tip (what map? i don’t even know where we are!)
Monday, August 1, 2011
THE SET CLASS




Monday, August 1, 2011
WHAT IS SET?

    • The          Set class is a Standard Lib class in Ruby

    • You          use it by requiring it explicitly ( require ‘set’ )

    • It     stores a collection of unordered, unique values

    • It     mixes-in the Enumerable module




Monday, August 1, 2011
SET IS ENUMERABLE
                         class Set
                           include Enumerable

                           #...

                           def each
                             block_given? or return enum_for(__method__)
                             @hash.each_key { |o| yield(o) }
                             self
                           end
                         end

        • Calls   a block for each member of the set passing the member
            as a parameter

        • Returns         an enumerator if no block is given
Monday, August 1, 2011
SET IS ENUMERABLE
                         class Set
                           include Enumerable

                           #...

                           def initialize
                             @hash ||= Hash.new
                             enum.nil? and return
                             if block
                               do_with_enum(enum) { |o| add(block[o]) }
                             else
                               merge(enum)
                             end
                           end
                         end

                • Actually    uses Hash for implementing unique values

Monday, August 1, 2011
SET IS ENUMERABLE
                         class Set
                           include Enumerable

                           #...

                           def include?
                             @hash.include?(o)
                           end
                           alias member? include?
                         end




                     • Defines     its own implementation of Enumerable methods


Monday, August 1, 2011
ARE YOU IN LOVE YET?




Monday, August 1, 2011
THEN READ THIS




      • Thistalk was inspired by the AWESOME discussion of
        Enumerable by David A. Black
      • READ IT! SPREAD THE WORD!!!!
Monday, August 1, 2011
THANK YOU!

                            hamin


                            harisamin


                            harisamin.tumblr.com



Monday, August 1, 2011

Mais conteúdo relacionado

Destaque

Network Layer,Computer Networks
Network Layer,Computer NetworksNetwork Layer,Computer Networks
Network Layer,Computer Networksguesta81d4b
 
Finite State Machine | Computer Science
Finite State Machine | Computer ScienceFinite State Machine | Computer Science
Finite State Machine | Computer ScienceTransweb Global Inc
 
BASIC CONCEPTS OF COMPUTER NETWORKS
BASIC CONCEPTS OF COMPUTER NETWORKS BASIC CONCEPTS OF COMPUTER NETWORKS
BASIC CONCEPTS OF COMPUTER NETWORKS Kak Yong
 
LinkedIn SlideShare: Knowledge, Well-Presented
LinkedIn SlideShare: Knowledge, Well-PresentedLinkedIn SlideShare: Knowledge, Well-Presented
LinkedIn SlideShare: Knowledge, Well-PresentedSlideShare
 

Destaque (7)

Network Layer,Computer Networks
Network Layer,Computer NetworksNetwork Layer,Computer Networks
Network Layer,Computer Networks
 
Finite state machine
Finite state machineFinite state machine
Finite state machine
 
Finite State Machine | Computer Science
Finite State Machine | Computer ScienceFinite State Machine | Computer Science
Finite State Machine | Computer Science
 
Cldch8
Cldch8Cldch8
Cldch8
 
Turing machine by_deep
Turing machine by_deepTuring machine by_deep
Turing machine by_deep
 
BASIC CONCEPTS OF COMPUTER NETWORKS
BASIC CONCEPTS OF COMPUTER NETWORKS BASIC CONCEPTS OF COMPUTER NETWORKS
BASIC CONCEPTS OF COMPUTER NETWORKS
 
LinkedIn SlideShare: Knowledge, Well-Presented
LinkedIn SlideShare: Knowledge, Well-PresentedLinkedIn SlideShare: Knowledge, Well-Presented
LinkedIn SlideShare: Knowledge, Well-Presented
 

Último

Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 

Último (20)

Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 

The Enumerable Module or How I Fell in Love with Ruby

  • 1. THE ENUMERABLE MODULE or How I Fell In Love with Ruby! Haris Amin Cascadia Ruby Conf 2011 07/29/2011 Monday, August 1, 2011
  • 2. WHO IS THIS GUY? • Haris Amin • Software/Web Developer • Live in New York City Monday, August 1, 2011
  • 3. THESE GUYS MAKE SURE I’M NOT LIVING IN THE STREETS Monday, August 1, 2011
  • 4. WHAT WE DO @ ? Shoulder Pressing Colleagues Healthy Programmer Vertebrates Monday, August 1, 2011
  • 5. LOOK MA I HAS DEGREE! • Studies Physics/Math in Undergrad •E = mc^2 , doesn’t pay for food • Programming, for me in college was just a means to compute something Monday, August 1, 2011
  • 6. LAST TALK I GAVE AT A CONFERENCE? • Simulation of Viscoelastic Fluids • What did I do? • Employed a weighted-norm least-squares finite element method to approximate the solution to Oldroyd-B equations • So...yeah... for me programming was just a way to compute stuff :) Monday, August 1, 2011
  • 7. THOUGHT TO MYSELF... Arrays & Hashes ARE FUN!!!! Monday, August 1, 2011
  • 8. WHAT IS THE ENUMERABLE MODULE? •A module, you can MIX IT IN! •A bunch of methods that work with collections • Empowers the most notably the Array and Hash classes (among others i.e. Set, Range, File, etc.) Monday, August 1, 2011
  • 9. HOW TO ‘MIX-IN’ THE ENUMERABLE? •A class ‘including’ or ‘mixing-in’ Enumerable must define the ‘#each’ method • Yielded items from the #each method empower the collection awareness for the class Monday, August 1, 2011
  • 10. class PlanetExpress include Enumerable def each yield “Bender” yield “Frye” yield “Leela” yield “Zoidberg” end end PlanetExpress.new.collect do |member| “#{member} works at Planet Express” end • The #collect method executes the provided block to all of the values yielded by #each Monday, August 1, 2011
  • 11. BUT WHY IS ENUMERABLE SO... ... ... ... SEXY!? Monday, August 1, 2011
  • 12. LOOK AT ALL THESE 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 Monday, August 1, 2011
  • 13. PROGRAMMER HAPPINESS “Programmers often feel joy when they can concentrate on the creative side of programming, so Ruby is designed to make programmers happy.” - Yukihiro Matsumoto (Matz) Monday, August 1, 2011
  • 14. WATCH OUT FOR DR. ZOIDBERG’S TIPS Monday, August 1, 2011
  • 15. LET’S TAKE A LOOK AT SOME ENUMERABLE METHODS... Monday, August 1, 2011
  • 16. each names = %w{ Frye Leela Zoidberg } names.each do |name| “#{name} works at Planet Express” end • The #each method yields items to a supplied block of code one at a time • Classes implement #each differently Monday, August 1, 2011
  • 17. find names = %w{ Frye Leela Zoidberg } names.find { |name| name.length > 4} • Elegantly simple, find one item that matches the condition supplied by the block • Consider how a library like ActiveRecord would reimplemnt find from Enumerable? Monday, August 1, 2011
  • 18. group_by names = %w{ Frye Bender Leela Zoidberg } names.group_by { |name| name.length} # => {4=>["Frye"], 6=>["Bender"], 5=>["Leela"], 8=>["Zoidberg"]} • It takes a block, and returns a hash, with the returned value from the block set as the key • Consider how one could use this as word count for a document/text Monday, August 1, 2011
  • 19. grep names = %w{ Frye Bender Leela Zoidberg } names.grep(/oidber/) # => ["Zoidberg"] • Searches for members of the collection according to a pattern.... pattern matching • It uses the === operator for pattern matching Monday, August 1, 2011
  • 20. GREP-ALICOUS! stuff = [ “Zoidberg”, Pizza.new, :homeless, “Dr."] stuff.grep(String) # => [ “Zoidberg”, “Dr.”] • Using the === allows us to do some fancy matching • We can grep for types or objects • Equivalent to stuff.select { |element| String === element} Dr. Zoidberg’s Tip (The doctor is in!) Monday, August 1, 2011
  • 21. map / collection names = %w{ Frye Bender Leela Zoidberg } names.map { |name| name.downcase } # => [“frye”, “bender”, “leela”, “zoidberg” ] • Think of it as a transformation method, that applies the block as a transformation • Always returns a new Array with the transformation applied • Different then #each, return value matters with #map Monday, August 1, 2011
  • 22. THE DELICIOUS ENUMERATOR ... enum ... enum ... enum Monday, August 1, 2011
  • 23. ENUMERATOR • We can create an Enumerator without mixing-in the Enumerable module and still have the power of Enumerable methods •3 ways to create Enumerator without mixing-in Enumerable 1. Create Enumerator explicitly with a code block 2. Attach an Enumerator to another object 3. Create Enumerator implicitly with blockless iterators Monday, August 1, 2011
  • 24. Create Enumerator explicitly with a code block e = Enumerator.new do |y| y << “Frye” y << “Bender” y << “Leela” y << “Zoidberg” end •y is the yielder, an instance of Enumerator::Yielder • You don’t yield from the block, you only append to the yielder Monday, August 1, 2011
  • 25. Attach an Enumerator to another object names = %w { Frye Bender Leela Zoidberg } e = names.enum_for(:select) • knows/learns how to implement #each from another object • we’re binding the Enumerator to the #select method of the names array Monday, August 1, 2011
  • 26. Create Enumerator implicitly with blockless iterators names = %w { Frye Leela Bender } names.enum_for(:select) # => #<Enumerator: ["Frye", "Leela", "Bender"]:map> names.map # => #<Enumerator: ["Frye", "Leela", "Bender"]:map> • most iterators when called without a block return an Enumerator • our blockless iterator returned the same Enumerator as the enum_for approach Monday, August 1, 2011
  • 27. BUT WHY DO WE CARE? ...USES? Monday, August 1, 2011
  • 28. Add Enumerability to an existing object module PlanetExpress class Ship PARTS= %w{ sprockets black-matter } def survey_parts PARTS.each {|part| yield part } end end end ship = PlanetExpress::Ship.new enum = ship.enum_for(:survey_parts) • Now we can use Enumerable methods on our ship object Monday, August 1, 2011
  • 29. Fine Grained Iteration scenes = %w{ credits opening climax end } e = scenes puts e.next puts e.next e.rewind puts e.next • An Enumerator is an object, it can maintain state • Think film reels, state machines, etc... Monday, August 1, 2011
  • 30. CHAINING ENUMERATORS... ...HMMM Monday, August 1, 2011
  • 31. CHAINING ENUMERATORS • Normally chaining enumerators isn’t very useful • names.map.select might as well be names.select • Most enumerators are just passing the array of values down the chain Monday, August 1, 2011
  • 32. LAZY SLICE names = %w { Frye Bender Leela Zoidberg } names.each_slice(2).map do |first, second| “#{first} gets a slice & #{second} gets a slice” end • Instead of creating a 2-element slices for the whole array in memory, the enumerator can create slices in a “lazy” manner and only create them as they are needed Dr. Zoidberg’s Tip (can i have a slice) Monday, August 1, 2011
  • 33. MAP WITH INDEX...WTF? names = %w { Leela Bender Frye Zoidberg } names.map.with_index do |name, i| “#{name} has a rank #{i}” end • There is no #map_with_index defined in Enumerable • Ah but we can chain... chain... chain! Dr. Zoidberg’s Tip (what map? i don’t even know where we are!) Monday, August 1, 2011
  • 34. THE SET CLASS Monday, August 1, 2011
  • 35. WHAT IS SET? • The Set class is a Standard Lib class in Ruby • You use it by requiring it explicitly ( require ‘set’ ) • It stores a collection of unordered, unique values • It mixes-in the Enumerable module Monday, August 1, 2011
  • 36. SET IS ENUMERABLE class Set include Enumerable #... def each block_given? or return enum_for(__method__) @hash.each_key { |o| yield(o) } self end end • Calls a block for each member of the set passing the member as a parameter • Returns an enumerator if no block is given Monday, August 1, 2011
  • 37. SET IS ENUMERABLE class Set include Enumerable #... def initialize @hash ||= Hash.new enum.nil? and return if block do_with_enum(enum) { |o| add(block[o]) } else merge(enum) end end end • Actually uses Hash for implementing unique values Monday, August 1, 2011
  • 38. SET IS ENUMERABLE class Set include Enumerable #... def include? @hash.include?(o) end alias member? include? end • Defines its own implementation of Enumerable methods Monday, August 1, 2011
  • 39. ARE YOU IN LOVE YET? Monday, August 1, 2011
  • 40. THEN READ THIS • Thistalk was inspired by the AWESOME discussion of Enumerable by David A. Black • READ IT! SPREAD THE WORD!!!! Monday, August 1, 2011
  • 41. THANK YOU! hamin harisamin harisamin.tumblr.com Monday, August 1, 2011