Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Inside Enumerable <ul><li>Mike Bowler </li></ul><ul><li>Gargoyle Software Inc. </li></ul>
Agenda <ul><li>What is Enumerable? </li></ul><ul><li>What can you do with it? </li></ul><ul><li>How does it do that? </li>...
What is it? <ul><li>It’s a group of related methods all having to do with collections </li></ul><ul><li>It’s the interesti...
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....
hash = { :a  => 1,  :b  => 2} hash.collect  do  |key, value|  value * 4 end array = [1,2,3] array.collect  do  |value| val...
multiple inheritance
mixins
Mixins Cannot be instantiated Can be mixed in
Mixins usually  don’t stand alone don’t stand alone <ul><li>Typically... </li></ul><ul><ul><li>mixins aren’t completely se...
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 *...
Enumerable.collect module  Enumerable def  collect array = [] each do |a| array <<  yield (a) end array end end class  Foo...
Enumerable.collect module  Enumerable def  collect array = [] each do |a| array <<  yield (a) end array end end class  Foo...
Enumerable.collect class  Foo include  Enumerable def  each yield 1 yield 2 yield 3 end end Foo.new.collect do |num| num *...
Enumerable.collect module  Enumerable def  collect array = [] each do |a| array <<  yield (a) end array end end class  Foo...
Enumerable.collect class  Foo include  Enumerable def  each yield 1 yield 2 yield 3 end end Foo.new.collect do |num| num *...
Required methods <ul><li>collect() would not have worked if Foo hadn’t declared each() </li></ul><ul><li>All methods in En...
Enumerable methods all?, any?, collect, detect, each_cons, each_slice, each_with_index, entries, enum_cons, enum_slice, en...
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 <ul><li>“Returns a new array with the results of running block once for every element in enum” </li></ul><ul>...
find, detect <ul><li>find(ifnone = nil) {| obj | block } => obj or nil </li></ul><ul><ul><li>Passes each entry in enum to ...
find_all, select <ul><li>find_all {| obj | block } => array </li></ul><ul><ul><li>Returns an array containing all elements...
grep <ul><li>grep(pattern) => array </li></ul><ul><ul><li>Returns an array of every element in enum for which Pattern === ...
Case equality <ul><li>For most objects, === is the same as == </li></ul>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 ...
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...
Wrap up <ul><li>Enumerable is already mixed into all the core collections </li></ul><ul><li>You can mix it into any of you...
Shameless Plug <ul><li>I can help you with your Ruby and/or Rails projects.  Ask me how. </li></ul><ul><li>Mike Bowler [em...
Upcoming SlideShare
Loading in …5
×

Inside Enumerable

3,422 views

Published on

A look at the Enumerable module in Ruby. What it is, how it works and what you can do with it.

Published in: Technology
  • Be the first to comment

Inside Enumerable

  1. 1. Inside Enumerable <ul><li>Mike Bowler </li></ul><ul><li>Gargoyle Software Inc. </li></ul>
  2. 2. Agenda <ul><li>What is Enumerable? </li></ul><ul><li>What can you do with it? </li></ul><ul><li>How does it do that? </li></ul>
  3. 3. What is it? <ul><li>It’s a group of related methods all having to do with collections </li></ul><ul><li>It’s the interesting part behind Array, Set, Hash and Range </li></ul><ul><li>It’s a Module (often called a mixin) </li></ul>
  4. 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. 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
  6. 6. multiple inheritance
  7. 7. mixins
  8. 8. Mixins Cannot be instantiated Can be mixed in
  9. 9. Mixins usually don’t stand alone don’t stand alone <ul><li>Typically... </li></ul><ul><ul><li>mixins aren’t completely self-contained </li></ul></ul><ul><ul><li>they rely on certain methods being present on the class they are mixed into </li></ul></ul><ul><li>This isn’t mandatory but is very common </li></ul><ul><li>Let’s look at an example... </li></ul>
  10. 10. Enumerable requires each() [1,2,3].collect {|i| i*2} [2,4,6]
  11. 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. 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. 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. 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. 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. 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. 17. Required methods <ul><li>collect() would not have worked if Foo hadn’t declared each() </li></ul><ul><li>All methods in Enumerable require each() </li></ul><ul><li>A couple also require <=> </li></ul><ul><ul><li>min(), max(), sort() </li></ul></ul>Comparison operator
  18. 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. 19. any? [1,2,3,4].any? {|a| a > 2} Returns true if this block... ...returns true for any of these values
  20. 20. all? [1,2,3,4].all? {|a| a > 2} Returns true if this block... ...returns true for ALL of these values
  21. 21. collect, map <ul><li>“Returns a new array with the results of running block once for every element in enum” </li></ul><ul><li>map is an alias for collect </li></ul>[1,2,3].collect {|i| i*2} [2,4,6]
  22. 22. find, detect <ul><li>find(ifnone = nil) {| obj | block } => obj or nil </li></ul><ul><ul><li>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 </li></ul></ul><ul><ul><li>detect is an alias for find </li></ul></ul>
  23. 23. find_all, select <ul><li>find_all {| obj | block } => array </li></ul><ul><ul><li>Returns an array containing all elements of enum for which block is not false </li></ul></ul><ul><ul><li>select is an alias for find_all </li></ul></ul>
  24. 24. grep <ul><li>grep(pattern) => array </li></ul><ul><ul><li>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 </li></ul></ul>Case equality
  25. 25. Case equality <ul><li>For most objects, === is the same as == </li></ul>Case equality Regular equality
  26. 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. 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. 28. Wrap up <ul><li>Enumerable is already mixed into all the core collections </li></ul><ul><li>You can mix it into any of your classes so long as you implement each() </li></ul><ul><li>It provides all kinds of useful methods to walk across the collections and get information out of them </li></ul>
  29. 29. Shameless Plug <ul><li>I can help you with your Ruby and/or Rails projects. Ask me how. </li></ul><ul><li>Mike Bowler [email_address] www.GargoyleSoftware.com (company) www.SphericalImprovement.com (blog) </li></ul>

×