A powerful Ruby Module for Collections<br />Enumerable<br />by Sarah Allen and Liah Hansen<br />
Why Use Enumerables<br />Ruby's Enumerable module has methods for all kinds of tasks which operate on a collection.<br />I...
What does Enumerable Mean?<br />Collection objects (instances of Array, Hash, etc) typically “mixin” the Enumerable module...
Mixing in Enumerable<br />class MyCollection<br />	include Enumerable<br />	#lots of code<br />	def each<br />		#more code...
View all Classes Mixing in Enumerable<br />ObjectSpace.each_object(Class) do |cl| <br />		puts cl if cl < Enumerable<br />...
Enumerable::Enumerator<br />Struct::Tms<br />Dir<br />File<br />IO<br />Range<br />Struct<br />Hash<br />Array<br />String...
Test an Instance or Class <br />>> a = [1,2,3]<br />=> [1, 2, 3]<br />>> a.respond_to? :any?<br />=> true<br />>> a.is_a? ...
Enumerable Methods<br />Collection Behavior<br />
each<br />Classes that include the Enumerable module must have an #each method. <br />The #each method yields items to a s...
map<br />The map method modifies each member according to instructions in a block and returns the modified collection of m...
grep<br />The grep method 'searches' for members using a regular expression. <br />>> v_names.grep /a/<br />=> ["car"]<br ...
find<br />>> v_names.find { |v| v.size > 3}<br />=> "truck"<br />>> v_names.find { |v| v.size > 2}<br />=> "car"<br />>> v...
all?<br />The all? method returns true if all of the members of a collection satisfy the evaluation of the block.  Otherwi...
any?<br />The any? method returns true if any of the members of a collection satisfy the evaluation of the block.  Otherwi...
Working with Complex Data<br />irb<br />>> load 'vehicles.rb'<br />=> true<br />
inject<br />>> $vehicles.inject(0) do |total_wheels, v|<br />?> total_wheels += v[:wheels]<br />>> end<br />=> 10<br />>> ...
Complex Operations<br />>> $vehicles.find do |v|<br />?> v[:name] =~ /Plane/<br />>> end[:name]<br />=> "Plane"<br />>> $v...
Complex Operations Continued<br />>> $vehicles.find_all do |v|<br />?> v[:classes].include? :ground<br />>> end.collect { ...
Upcoming SlideShare
Loading in …5
×

Ruby Enumerable

4,151
-1

Published on

Ruby's Enumerable module

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
4,151
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
35
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • its fine to use is_a in irb..in code should use respond_to?
  • Our first example below returns any member which contains an &apos;a&apos;. The grep method also accepts a block, which is passed each matching value, &apos;collecting&apos; the results returned and returning those as shown in the second example.
  • Ruby Enumerable

    1. 1. A powerful Ruby Module for Collections<br />Enumerable<br />by Sarah Allen and Liah Hansen<br />
    2. 2. Why Use Enumerables<br />Ruby's Enumerable module has methods for all kinds of tasks which operate on a collection.<br />If you can imagine a use for the #each method other than simply iterating, there is a good chance a method exists to do what you have in mind. <br />
    3. 3. What does Enumerable Mean?<br />Collection objects (instances of Array, Hash, etc) typically “mixin” the Enumerable module<br />The Enumerable module gives objects of collection classes additional collection-specific behaviors.<br />The class requiring the Enumerable module must have an #each method because the additional collection-specific behaviors given by Enumerable are defined in terms of #each<br />
    4. 4. Mixing in Enumerable<br />class MyCollection<br /> include Enumerable<br /> #lots of code<br /> def each<br /> #more code<br /> end<br />end<br />
    5. 5. View all Classes Mixing in Enumerable<br />ObjectSpace.each_object(Class) do |cl| <br /> puts cl if cl < Enumerable<br />end<br />
    6. 6. Enumerable::Enumerator<br />Struct::Tms<br />Dir<br />File<br />IO<br />Range<br />Struct<br />Hash<br />Array<br />String<br />Struct::Group<br />Struct::Passwd<br />MyCollection<br />StringIO<br />Gem::SourceIndex<br />YAML::Set<br />YAML::Pairs<br />YAML::Omap<br />YAML::SpecialHas<br />
    7. 7. Test an Instance or Class <br />>> a = [1,2,3]<br />=> [1, 2, 3]<br />>> a.respond_to? :any?<br />=> true<br />>> a.is_a? Enumerable<br />=> true<br />>> Array < Enumerable<br />=> true<br />
    8. 8. Enumerable Methods<br />Collection Behavior<br />
    9. 9. each<br />Classes that include the Enumerable module must have an #each method. <br />The #each method yields items to a supplied code block, one at a time<br />Different Classes define #each differently<br />Array: #each yields each element<br />Hash: each yields #each key/value pair as a two-element array<br />>> v_names = %w(car truck bike)<br />=> ["car", "truck", "bike"]<br />>> v_names.each do |vehicle|<br />?> puts vehicle<br />>> end<br />
    10. 10. map<br />The map method modifies each member according to instructions in a block and returns the modified collection of members.<br />>> v_names.map { |v| v.upcase}<br />=> ["CAR", "TRUCK", "BIKE"]<br />
    11. 11. grep<br />The grep method 'searches' for members using a regular expression. <br />>> v_names.grep /a/<br />=> ["car"]<br />>> v_names.grep(/a/) { |v| v.upcase}<br />=> ["CAR"]<br />
    12. 12. find<br />>> v_names.find { |v| v.size > 3}<br />=> "truck"<br />>> v_names.find { |v| v.size > 2}<br />=> "car"<br />>> v_names.find do |v| <br />v.size > 3 && v.size < 5<br />end<br />=> "bike"<br />
    13. 13. all?<br />The all? method returns true if all of the members of a collection satisfy the evaluation of the block. Otherwise it returns false.<br />>> v_names.all? { |v| v.length > 2}<br />=> true<br />>> v_names.all? { |v| v.length > 10}<br />=> false<br />
    14. 14. any?<br />The any? method returns true if any of the members of a collection satisfy the evaluation of the block. Otherwise it returns false.<br />>> v_names.any? { |v| v.length == 3}<br />=> true<br />>> v_names.any? { |v| v = "car"}<br />=> true<br />
    15. 15. Working with Complex Data<br />irb<br />>> load 'vehicles.rb'<br />=> true<br />
    16. 16. inject<br />>> $vehicles.inject(0) do |total_wheels, v|<br />?> total_wheels += v[:wheels]<br />>> end<br />=> 10<br />>> $vehicles.inject([]) do |classes, v|<br />?> classes += v[:classes]<br />>> end.uniq<br />=> [:ground, :water, :air]<br />
    17. 17. Complex Operations<br />>> $vehicles.find do |v|<br />?> v[:name] =~ /Plane/<br />>> end[:name]<br />=> "Plane"<br />>> $vehicles.find_all do |v|<br />?> v[:name] =~ /Plane/<br />>> end.collect { |v| v[:name] }<br />=> ["Plane", "Sea Plane"]<br />>> $vehicles.find_all do |v|<br />?> v[:wheels] > 0 <br />>> end.collect { |v| v[:name] }<br />=> ["Car", "Truck", "Bike"]<br />
    18. 18. Complex Operations Continued<br />>> $vehicles.find_all do |v|<br />?> v[:classes].include? :ground<br />>> end.collect { |v| v[:name] }<br />=> ["Car", "Truck", "Bike", "Sea Plane"]<br />>> $vehicles.find_all do |v|<br />?> v[:classes].include? :air<br />>> end.collect { |v| v[:name] }<br />=> ["Plane", "Helicopter", "Sea Plane"]<br />
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×