0
ENUMERABLE                        A powerful Ruby Module for CollectionsFriday, March 4, 2011
WHY USE ENUMERABLES  • Rubys Enumerable module has methods for all kinds of tasks    which operate on a collection.  • If ...
WHAT DOES ENUMERABLE                MEAN?  • Collection objects (instances of Array, Hash, etc) typically “mixin”    the E...
MIXING IN ENUMERABLE  class MyCollection  ! include Enumerable  ! #lots of code  !  ! def each  ! ! #more code  ! end  end...
VIEW ALL CLASSES MIXING IN                       ENUMERABLE  ObjectSpace.each_object(Class) do |cl|  ! ! puts cl if cl < E...
Enumerable::Enumerator Struct::Tms Dir File IO Range Struct Hash Array String Struct::Group Struct::Passwd MyCollection St...
TEST AN INSTANCE OR CLASS  >> a = [1,2,3]  => [1, 2, 3]  >> a.respond_to? :any?  => true  >> a.is_a? Enumerable  => true  ...
ENUMERABLE METHODS  • Collection BehaviorFriday, March 4, 2011
EACH  • Classes that include the Enumerable module must have an #each    method.  • The #each method yields items to a sup...
MAP  • The map method modifies each member according to    instructions in a block and returns the modified collection of   ...
GREP  • The grep method searches for members using a regular    expression.  >> v_names.grep /a/  => ["car"]  >> v_names.g...
FIND  >> v_names.find { |v| v.size > 3}  => "truck"  >> v_names.find { |v| v.size > 2}  => "car"  >> v_names.find do |v|  ...
ALL?  • The all? method returns true if all of the members of a collection    satisfy the evaluation of the block. Otherwi...
ANY?  • The any? method returns true if any of the members of a    collection satisfy the evaluation of the block. Otherwi...
WORKING WITH COMPLEX                 DATA  irb  >> load vehicles.rb  => trueFriday, March 4, 2011
INJECT  >>     $vehicles.inject(0) do |total_wheels, v|  ?>     total_wheels += v[:wheels]  >>     end  =>     10  >>     ...
COMPLEX OPERATIONS  >>      $vehicles.find do |v|  ?>      v[:name] =~ /Plane/  >>      end[:name]  =>      "Plane"  >>   ...
COMPLEX OPERATIONS                   CONTINUED  >>     $vehicles.find_all do |v|  ?>     v[:classes].include? :ground  >> ...
Upcoming SlideShare
Loading in...5
×

Enumerables

700

Published on

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
700
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
17
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Enumerables"

  1. 1. ENUMERABLE A powerful Ruby Module for CollectionsFriday, March 4, 2011
  2. 2. WHY USE ENUMERABLES • Rubys Enumerable module has methods for all kinds of tasks which operate on a collection. • 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.Friday, March 4, 2011
  3. 3. WHAT DOES ENUMERABLE MEAN? • Collection objects (instances of Array, Hash, etc) typically “mixin” the Enumerable module • The Enumerable module gives objects of collection classes additional collection-specific behaviors. • 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 #eachFriday, March 4, 2011
  4. 4. MIXING IN ENUMERABLE class MyCollection ! include Enumerable ! #lots of code ! ! def each ! ! #more code ! end endFriday, March 4, 2011
  5. 5. VIEW ALL CLASSES MIXING IN ENUMERABLE ObjectSpace.each_object(Class) do |cl| ! ! puts cl if cl < Enumerable endFriday, March 4, 2011
  6. 6. Enumerable::Enumerator Struct::Tms Dir File IO Range Struct Hash Array String Struct::Group Struct::Passwd MyCollection StringIO Gem::SourceIndex YAML::Set YAML::Pairs YAML::Omap YAML::SpecialHasFriday, March 4, 2011
  7. 7. TEST AN INSTANCE OR CLASS >> a = [1,2,3] => [1, 2, 3] >> a.respond_to? :any? => true >> a.is_a? Enumerable => true >> Array < Enumerable => trueFriday, March 4, 2011
  8. 8. ENUMERABLE METHODS • Collection BehaviorFriday, March 4, 2011
  9. 9. EACH • Classes that include the Enumerable module must have an #each method. • The #each method yields items to a supplied code block, one at a time • Different Classes define #each differently • Array: #each yields each element • Hash: each yields #each key/value pair as a two-element array >> v_names = %w(car truck bike) => ["car", "truck", "bike"] >> v_names.each do |vehicle| ?> puts vehicle >> endFriday, March 4, 2011
  10. 10. MAP • The map method modifies each member according to instructions in a block and returns the modified collection of members. >> v_names.map { |v| v.upcase} => ["CAR", "TRUCK", "BIKE"]Friday, March 4, 2011
  11. 11. GREP • The grep method searches for members using a regular expression. >> v_names.grep /a/ => ["car"] >> v_names.grep(/a/) { |v| v.upcase} => ["CAR"]Friday, March 4, 2011
  12. 12. FIND >> v_names.find { |v| v.size > 3} => "truck" >> v_names.find { |v| v.size > 2} => "car" >> v_names.find do |v| v.size > 3 && v.size < 5 end => "bike"Friday, March 4, 2011
  13. 13. ALL? • The all? method returns true if all of the members of a collection satisfy the evaluation of the block. Otherwise it returns false. >> v_names.all? { |v| v.length > 2} => true >> v_names.all? { |v| v.length > 10} => falseFriday, March 4, 2011
  14. 14. ANY? • The any? method returns true if any of the members of a collection satisfy the evaluation of the block. Otherwise it returns false. >> v_names.any? { |v| v.length == 3} => true >> v_names.any? { |v| v = "car"} => trueFriday, March 4, 2011
  15. 15. WORKING WITH COMPLEX DATA irb >> load vehicles.rb => trueFriday, March 4, 2011
  16. 16. INJECT >> $vehicles.inject(0) do |total_wheels, v| ?> total_wheels += v[:wheels] >> end => 10 >> $vehicles.inject([]) do |classes, v| ?> classes += v[:classes] >> end.uniq => [:ground, :water, :air]Friday, March 4, 2011
  17. 17. COMPLEX OPERATIONS >> $vehicles.find do |v| ?> v[:name] =~ /Plane/ >> end[:name] => "Plane" >> $vehicles.find_all do |v| ?> v[:name] =~ /Plane/ >> end.collect { |v| v[:name] } => ["Plane", "Sea Plane"] >> $vehicles.find_all do |v| ?> v[:wheels] > 0 >> end.collect { |v| v[:name] } => ["Car", "Truck", "Bike"]Friday, March 4, 2011
  18. 18. COMPLEX OPERATIONS CONTINUED >> $vehicles.find_all do |v| ?> v[:classes].include? :ground >> end.collect { |v| v[:name] } => ["Car", "Truck", "Bike", "Sea Plane"] >> $vehicles.find_all do |v| ?> v[:classes].include? :air >> end.collect { |v| v[:name] } => ["Plane", "Helicopter", "Sea Plane"]Friday, March 4, 2011
  1. A particular slide catching your eye?

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

×