SlideShare a Scribd company logo
1 of 3
Enumerables Ruby's Enumerable module has methods for all kinds of tasks. Most likely if you can imagine a use for the #each method other than simply iterating, there is a good change a method exists to do what you had in mind. Arrays and hashes are Enumerable.  To see if you can call these methods, you can check if an instance responds to a particular method (preferred in code) or you can test the instance or class: >> a = [1,2,3] => [1, 2, 3] >> a.respond_to? :any? => true >> a.is_a? Enumerable => true >> Array < Enumerable => true Try this to see all of the classes: >> ObjectSpace.each_object(Class) {|cl| puts cl if cl < Enumerable} Our test subject for the next few examples will be the following array: vehicles = %w[ car truck boat plane helicopter bike ] Standard Iteration With Each Standard iteration is performed using the each method. This is typical in many languages. For instance in PHP this would be for each however in Ruby this is not built-in this is a method call on the vehicles array object. The sample code below simply outputs a list of our vehicles. vehicles.each do |vehicle|  puts vehicleend Modifying Every Member with Map The map method allows us to modify each member in the same way and return a new collection with new members that were produced by the code inside our block. >> vehicles.map { |v| v.upcase } => [
CAR
, 
TRUCK
, 
BOAT
, 
PLANE
, 
HELICOPTER
, 
BIKE
] Searching Members With Grep The grep method allows us to 'search' for members using a regular expression. Our first example below returns any member which contains an 'a'. The grep method also accepts a block, which is passed each matching value, 'collecting' the results returned and returning those as shown in the second example. vehicles.grep /a/ # => [
car
, 
boat
, 
plane
]vehicles.grep(/a/) { |v| v.upcase } # => [
CAR
, 
BOAT
, 
PLANE
] Evaluating All Members With All? The all? method accepts a block and simply returns either true or false based on the evaluation within the block. vehicles.all? { |v| v.length >= 3 }# => truevehicles.all? { |v| v.length < 2 }# => false Checking Evaluation For A Single Using Any? The any? method compliments all? in the fact that when the block evaluates to true at any time, then true is returned. vehicles.any? { |v| v.length == 3 }# => truevehicles.any? { |v| v.length > 10 }# => false Working With Complex Data For our next examples we will be working with vehicles as well, however more complex data structures using Hashes.   cd 08_enumerables irb >> load ‘vehicles.rb’ >> $vehicles  Collecting A List The collect method is meant for this very task. Collect accepts a block whose values are collected into an array. This is commonly used in conjunction with the join method to create strings from complex data. $vehicles.collect { |v| v[:name] } # => [
Car
, 
Truck
, 
Boat
, 
Plane
, 
Helicopter
, 
Bike
, 
Sea Plane
] $vehicles.collect { |v| v[:name] }.join ', ' # => 
Car, Truck, Boat, Plane, Helicopter, Bike, Sea Plane
 Finding Members Using The Find Method The find and find_all methods are the same although different in the obvious fact that one halts iteration after it finds a member, the other continues and finds the rest. Consider the following examples, we are simply trying to find members that match names, have many wheels, or are ground or air based. The collect method is used to collect arrays of the names for demonstration display purposes, instead of displaying data from the #inspect method. $vehicles.find { |v| v[:name] =~ /Plane/ }[:name]# => 
Plane
$vehicles.find_all { |v| v[:name] =~ /Plane/ }.collect { |v| v[:name] } # => [
Plane
, 
Sea Plane
]$vehicles.find_all { |v| v[:wheels] > 0 }.collect { |v| v[:name] } # => [
Car
, 
Truck
, 
Bike
]$vehicles.find_all { |v| v[:classes].include? :ground }.collect { |v| v[:name] } # => [
Car
, 
Truck
, 
Bike
, 
Sea Plane
]$vehicles.find_all { |v| v[:classes].include? :air }.collect { |v| v[:name] } # => [
Plane
, 
Helicopter
, 
Sea Plane
] Iterating With Storage Using Inject When you are looking to collect values during iteration the inject method is the perfect one for the job. This method accepts a initialization parameter which is 0 and [] in the case below, this is then passed p $vehicles.inject(0) { |total_wheels, v| total_wheels += v[:wheels] } # => 10p $vehicles.inject([]) { |classes, v| classes + v[:classes] }.uniq # => [:ground, :water, :air] Sources This document is based on this excellent post: http://vision-media.ca/resources/ruby/ruby-enumerable-method-examples Edited and added to by Sarah Allen, blazingcloud.net Please also see reference docs: http://ruby-doc.org/core/classes/Enumerable.html
Enumerables
Enumerables

More Related Content

What's hot

Applicative Functor - Part 3
Applicative Functor - Part 3Applicative Functor - Part 3
Applicative Functor - Part 3Philip Schwarz
 
JavaScript regular expression
JavaScript regular expressionJavaScript regular expression
JavaScript regular expressionHernan Mammana
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With ScalaKnoldus Inc.
 
PCA and LDA in machine learning
PCA and LDA in machine learningPCA and LDA in machine learning
PCA and LDA in machine learningAkhilesh Joshi
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2Philip Schwarz
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 4
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 4Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 4
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 4Philip Schwarz
 
Monoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and CatsMonoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and CatsPhilip Schwarz
 
Perls Functional functions
Perls Functional functionsPerls Functional functions
Perls Functional functionsdaoswald
 
Implicit conversion and parameters
Implicit conversion and parametersImplicit conversion and parameters
Implicit conversion and parametersKnoldus Inc.
 
Introducing Assignment invalidates the Substitution Model of Evaluation and v...
Introducing Assignment invalidates the Substitution Model of Evaluation and v...Introducing Assignment invalidates the Substitution Model of Evaluation and v...
Introducing Assignment invalidates the Substitution Model of Evaluation and v...Philip Schwarz
 
Monad Laws Must be Checked
Monad Laws Must be CheckedMonad Laws Must be Checked
Monad Laws Must be CheckedPhilip Schwarz
 
2 R Tutorial Programming
2 R Tutorial Programming2 R Tutorial Programming
2 R Tutorial ProgrammingSakthi Dasans
 
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading Charndeep Sekhon
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...Philip Schwarz
 

What's hot (17)

Recursion Lecture in C++
Recursion Lecture in C++Recursion Lecture in C++
Recursion Lecture in C++
 
Applicative Functor - Part 3
Applicative Functor - Part 3Applicative Functor - Part 3
Applicative Functor - Part 3
 
JavaScript regular expression
JavaScript regular expressionJavaScript regular expression
JavaScript regular expression
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With Scala
 
PCA and LDA in machine learning
PCA and LDA in machine learningPCA and LDA in machine learning
PCA and LDA in machine learning
 
C# p8
C# p8C# p8
C# p8
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 4
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 4Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 4
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 4
 
Monoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and CatsMonoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and Cats
 
Perls Functional functions
Perls Functional functionsPerls Functional functions
Perls Functional functions
 
Implicit conversion and parameters
Implicit conversion and parametersImplicit conversion and parameters
Implicit conversion and parameters
 
Introducing Assignment invalidates the Substitution Model of Evaluation and v...
Introducing Assignment invalidates the Substitution Model of Evaluation and v...Introducing Assignment invalidates the Substitution Model of Evaluation and v...
Introducing Assignment invalidates the Substitution Model of Evaluation and v...
 
Monad Laws Must be Checked
Monad Laws Must be CheckedMonad Laws Must be Checked
Monad Laws Must be Checked
 
Knolx session
Knolx sessionKnolx session
Knolx session
 
2 R Tutorial Programming
2 R Tutorial Programming2 R Tutorial Programming
2 R Tutorial Programming
 
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
 

Viewers also liked

Streamlined Geek Talk
Streamlined Geek TalkStreamlined Geek Talk
Streamlined Geek TalkSarah Allen
 
Ruby performance
Ruby performanceRuby performance
Ruby performanceSarah Allen
 
The Making of Pie
The Making of PieThe Making of Pie
The Making of PieSarah Allen
 
Sarah Allen Computer Science Entrepreneur
Sarah Allen Computer Science EntrepreneurSarah Allen Computer Science Entrepreneur
Sarah Allen Computer Science EntrepreneurSarah Allen
 
Blazing Cloud: Agile Product Development
Blazing Cloud: Agile Product DevelopmentBlazing Cloud: Agile Product Development
Blazing Cloud: Agile Product DevelopmentSarah Allen
 

Viewers also liked (7)

Streamlined Geek Talk
Streamlined Geek TalkStreamlined Geek Talk
Streamlined Geek Talk
 
Ruby Enumerable
Ruby EnumerableRuby Enumerable
Ruby Enumerable
 
Ruby performance
Ruby performanceRuby performance
Ruby performance
 
The Making of Pie
The Making of PieThe Making of Pie
The Making of Pie
 
Intro To Ruby
Intro To RubyIntro To Ruby
Intro To Ruby
 
Sarah Allen Computer Science Entrepreneur
Sarah Allen Computer Science EntrepreneurSarah Allen Computer Science Entrepreneur
Sarah Allen Computer Science Entrepreneur
 
Blazing Cloud: Agile Product Development
Blazing Cloud: Agile Product DevelopmentBlazing Cloud: Agile Product Development
Blazing Cloud: Agile Product Development
 

Similar to Enumerables

How To Test Everything
How To Test EverythingHow To Test Everything
How To Test Everythingnoelrap
 
RubyMiniGuide-v1.0_0
RubyMiniGuide-v1.0_0RubyMiniGuide-v1.0_0
RubyMiniGuide-v1.0_0tutorialsruby
 
RubyMiniGuide-v1.0_0
RubyMiniGuide-v1.0_0RubyMiniGuide-v1.0_0
RubyMiniGuide-v1.0_0tutorialsruby
 
Goal The goal of this assignment is to help students understand the.pdf
Goal The goal of this assignment is to help students understand the.pdfGoal The goal of this assignment is to help students understand the.pdf
Goal The goal of this assignment is to help students understand the.pdfarsmobiles
 
javaloop understanding what is java.pptx
javaloop understanding what is java.pptxjavaloop understanding what is java.pptx
javaloop understanding what is java.pptxRobertCarreonBula
 
Ruby on Rails Intro
Ruby on Rails IntroRuby on Rails Intro
Ruby on Rails Introzhang tao
 
CIS 407 STUDY Inspiring Innovation--cis407study.com
CIS 407 STUDY Inspiring Innovation--cis407study.comCIS 407 STUDY Inspiring Innovation--cis407study.com
CIS 407 STUDY Inspiring Innovation--cis407study.comKeatonJennings91
 
VB_ERROR CONTROL_FILE HANDLING.ppt
VB_ERROR CONTROL_FILE HANDLING.pptVB_ERROR CONTROL_FILE HANDLING.ppt
VB_ERROR CONTROL_FILE HANDLING.pptBhuvanaR13
 
You are given a specification for some Java classes as follows.  A.pdf
You are given a specification for some Java classes as follows.  A.pdfYou are given a specification for some Java classes as follows.  A.pdf
You are given a specification for some Java classes as follows.  A.pdfeyebolloptics
 
Functional Javascript
Functional JavascriptFunctional Javascript
Functional Javascriptguest4d57e6
 
Introduction to Ruby’s Reflection API
Introduction to Ruby’s Reflection APIIntroduction to Ruby’s Reflection API
Introduction to Ruby’s Reflection APINiranjan Sarade
 
Groovy Api Tutorial
Groovy Api  TutorialGroovy Api  Tutorial
Groovy Api Tutorialguligala
 

Similar to Enumerables (20)

Enumerables
EnumerablesEnumerables
Enumerables
 
How To Test Everything
How To Test EverythingHow To Test Everything
How To Test Everything
 
RubyMiniGuide-v1.0_0
RubyMiniGuide-v1.0_0RubyMiniGuide-v1.0_0
RubyMiniGuide-v1.0_0
 
RubyMiniGuide-v1.0_0
RubyMiniGuide-v1.0_0RubyMiniGuide-v1.0_0
RubyMiniGuide-v1.0_0
 
Goal The goal of this assignment is to help students understand the.pdf
Goal The goal of this assignment is to help students understand the.pdfGoal The goal of this assignment is to help students understand the.pdf
Goal The goal of this assignment is to help students understand the.pdf
 
JAVA LOOP.pptx
JAVA LOOP.pptxJAVA LOOP.pptx
JAVA LOOP.pptx
 
javaloop understanding what is java.pptx
javaloop understanding what is java.pptxjavaloop understanding what is java.pptx
javaloop understanding what is java.pptx
 
Enumerable
EnumerableEnumerable
Enumerable
 
Ruby on Rails Intro
Ruby on Rails IntroRuby on Rails Intro
Ruby on Rails Intro
 
Computer programming 2 Lesson 13
Computer programming 2  Lesson 13Computer programming 2  Lesson 13
Computer programming 2 Lesson 13
 
CIS 407 STUDY Inspiring Innovation--cis407study.com
CIS 407 STUDY Inspiring Innovation--cis407study.comCIS 407 STUDY Inspiring Innovation--cis407study.com
CIS 407 STUDY Inspiring Innovation--cis407study.com
 
VB_ERROR CONTROL_FILE HANDLING.ppt
VB_ERROR CONTROL_FILE HANDLING.pptVB_ERROR CONTROL_FILE HANDLING.ppt
VB_ERROR CONTROL_FILE HANDLING.ppt
 
You are given a specification for some Java classes as follows.  A.pdf
You are given a specification for some Java classes as follows.  A.pdfYou are given a specification for some Java classes as follows.  A.pdf
You are given a specification for some Java classes as follows.  A.pdf
 
Functional Javascript
Functional JavascriptFunctional Javascript
Functional Javascript
 
WD programs descriptions.docx
WD programs descriptions.docxWD programs descriptions.docx
WD programs descriptions.docx
 
Introduction to Ruby’s Reflection API
Introduction to Ruby’s Reflection APIIntroduction to Ruby’s Reflection API
Introduction to Ruby’s Reflection API
 
Laravel Unit Testing
Laravel Unit TestingLaravel Unit Testing
Laravel Unit Testing
 
E4
E4E4
E4
 
Groovy Api Tutorial
Groovy Api  TutorialGroovy Api  Tutorial
Groovy Api Tutorial
 
10- java language basics part4
10- java language basics part410- java language basics part4
10- java language basics part4
 

More from Sarah Allen

Internet security: a landscape of unintended consequences
Internet security: a landscape of unintended consequencesInternet security: a landscape of unintended consequences
Internet security: a landscape of unintended consequencesSarah Allen
 
RTMP: how did we get to now? (Demuxed 2019)
RTMP: how did we get to now? (Demuxed 2019)RTMP: how did we get to now? (Demuxed 2019)
RTMP: how did we get to now? (Demuxed 2019)Sarah Allen
 
Communication is a Technical Skill
Communication is a Technical SkillCommunication is a Technical Skill
Communication is a Technical SkillSarah Allen
 
Improving Federal Government Services
Improving Federal Government ServicesImproving Federal Government Services
Improving Federal Government ServicesSarah Allen
 
Transparency Wins
Transparency WinsTransparency Wins
Transparency WinsSarah Allen
 
A Short History of Computers
A Short History of ComputersA Short History of Computers
A Short History of ComputersSarah Allen
 
Making Software Fun
Making Software FunMaking Software Fun
Making Software FunSarah Allen
 
Power of Transparency
Power of TransparencyPower of Transparency
Power of TransparencySarah Allen
 
Designing for Fun
Designing for FunDesigning for Fun
Designing for FunSarah Allen
 
Ruby in the US Government for Ruby World Conference
Ruby in the US Government for Ruby World ConferenceRuby in the US Government for Ruby World Conference
Ruby in the US Government for Ruby World ConferenceSarah Allen
 
Identities of Dead People
Identities of Dead PeopleIdentities of Dead People
Identities of Dead PeopleSarah Allen
 
3 Reasons Not to Use Ruby
3 Reasons Not to Use Ruby 3 Reasons Not to Use Ruby
3 Reasons Not to Use Ruby Sarah Allen
 
Ruby Nation: Why no haz Ruby?
Ruby Nation: Why no haz Ruby?Ruby Nation: Why no haz Ruby?
Ruby Nation: Why no haz Ruby?Sarah Allen
 
Why no ruby in gov?
Why no ruby in gov?Why no ruby in gov?
Why no ruby in gov?Sarah Allen
 
People Patterns or What I learned from Toastmasters
People Patterns or What I learned from ToastmastersPeople Patterns or What I learned from Toastmasters
People Patterns or What I learned from ToastmastersSarah Allen
 
Crowdsourced Transcription Landscape
Crowdsourced Transcription LandscapeCrowdsourced Transcription Landscape
Crowdsourced Transcription LandscapeSarah Allen
 
Lessons Learned Future Thoughts
Lessons Learned Future ThoughtsLessons Learned Future Thoughts
Lessons Learned Future ThoughtsSarah Allen
 
Mobile Web Video
Mobile Web VideoMobile Web Video
Mobile Web VideoSarah Allen
 
Elementary Computer History
Elementary Computer HistoryElementary Computer History
Elementary Computer HistorySarah Allen
 

More from Sarah Allen (20)

Internet security: a landscape of unintended consequences
Internet security: a landscape of unintended consequencesInternet security: a landscape of unintended consequences
Internet security: a landscape of unintended consequences
 
RTMP: how did we get to now? (Demuxed 2019)
RTMP: how did we get to now? (Demuxed 2019)RTMP: how did we get to now? (Demuxed 2019)
RTMP: how did we get to now? (Demuxed 2019)
 
Communication is a Technical Skill
Communication is a Technical SkillCommunication is a Technical Skill
Communication is a Technical Skill
 
Improving Federal Government Services
Improving Federal Government ServicesImproving Federal Government Services
Improving Federal Government Services
 
Transparency Wins
Transparency WinsTransparency Wins
Transparency Wins
 
A Short History of Computers
A Short History of ComputersA Short History of Computers
A Short History of Computers
 
Making Software Fun
Making Software FunMaking Software Fun
Making Software Fun
 
Power of Transparency
Power of TransparencyPower of Transparency
Power of Transparency
 
Designing for Fun
Designing for FunDesigning for Fun
Designing for Fun
 
Ruby in the US Government for Ruby World Conference
Ruby in the US Government for Ruby World ConferenceRuby in the US Government for Ruby World Conference
Ruby in the US Government for Ruby World Conference
 
Identities of Dead People
Identities of Dead PeopleIdentities of Dead People
Identities of Dead People
 
Let's pretend
Let's pretendLet's pretend
Let's pretend
 
3 Reasons Not to Use Ruby
3 Reasons Not to Use Ruby 3 Reasons Not to Use Ruby
3 Reasons Not to Use Ruby
 
Ruby Nation: Why no haz Ruby?
Ruby Nation: Why no haz Ruby?Ruby Nation: Why no haz Ruby?
Ruby Nation: Why no haz Ruby?
 
Why no ruby in gov?
Why no ruby in gov?Why no ruby in gov?
Why no ruby in gov?
 
People Patterns or What I learned from Toastmasters
People Patterns or What I learned from ToastmastersPeople Patterns or What I learned from Toastmasters
People Patterns or What I learned from Toastmasters
 
Crowdsourced Transcription Landscape
Crowdsourced Transcription LandscapeCrowdsourced Transcription Landscape
Crowdsourced Transcription Landscape
 
Lessons Learned Future Thoughts
Lessons Learned Future ThoughtsLessons Learned Future Thoughts
Lessons Learned Future Thoughts
 
Mobile Web Video
Mobile Web VideoMobile Web Video
Mobile Web Video
 
Elementary Computer History
Elementary Computer HistoryElementary Computer History
Elementary Computer History
 

Enumerables

  • 1. Enumerables Ruby's Enumerable module has methods for all kinds of tasks. Most likely if you can imagine a use for the #each method other than simply iterating, there is a good change a method exists to do what you had in mind. Arrays and hashes are Enumerable. To see if you can call these methods, you can check if an instance responds to a particular method (preferred in code) or you can test the instance or class: >> a = [1,2,3] => [1, 2, 3] >> a.respond_to? :any? => true >> a.is_a? Enumerable => true >> Array < Enumerable => true Try this to see all of the classes: >> ObjectSpace.each_object(Class) {|cl| puts cl if cl < Enumerable} Our test subject for the next few examples will be the following array: vehicles = %w[ car truck boat plane helicopter bike ] Standard Iteration With Each Standard iteration is performed using the each method. This is typical in many languages. For instance in PHP this would be for each however in Ruby this is not built-in this is a method call on the vehicles array object. The sample code below simply outputs a list of our vehicles. vehicles.each do |vehicle|  puts vehicleend Modifying Every Member with Map The map method allows us to modify each member in the same way and return a new collection with new members that were produced by the code inside our block. >> vehicles.map { |v| v.upcase } => [ CAR , TRUCK , BOAT , PLANE , HELICOPTER , BIKE ] Searching Members With Grep The grep method allows us to 'search' for members using a regular expression. Our first example below returns any member which contains an 'a'. The grep method also accepts a block, which is passed each matching value, 'collecting' the results returned and returning those as shown in the second example. vehicles.grep /a/ # => [ car , boat , plane ]vehicles.grep(/a/) { |v| v.upcase } # => [ CAR , BOAT , PLANE ] Evaluating All Members With All? The all? method accepts a block and simply returns either true or false based on the evaluation within the block. vehicles.all? { |v| v.length >= 3 }# => truevehicles.all? { |v| v.length < 2 }# => false Checking Evaluation For A Single Using Any? The any? method compliments all? in the fact that when the block evaluates to true at any time, then true is returned. vehicles.any? { |v| v.length == 3 }# => truevehicles.any? { |v| v.length > 10 }# => false Working With Complex Data For our next examples we will be working with vehicles as well, however more complex data structures using Hashes. cd 08_enumerables irb >> load ‘vehicles.rb’ >> $vehicles Collecting A List The collect method is meant for this very task. Collect accepts a block whose values are collected into an array. This is commonly used in conjunction with the join method to create strings from complex data. $vehicles.collect { |v| v[:name] } # => [ Car , Truck , Boat , Plane , Helicopter , Bike , Sea Plane ] $vehicles.collect { |v| v[:name] }.join ', ' # => Car, Truck, Boat, Plane, Helicopter, Bike, Sea Plane Finding Members Using The Find Method The find and find_all methods are the same although different in the obvious fact that one halts iteration after it finds a member, the other continues and finds the rest. Consider the following examples, we are simply trying to find members that match names, have many wheels, or are ground or air based. The collect method is used to collect arrays of the names for demonstration display purposes, instead of displaying data from the #inspect method. $vehicles.find { |v| v[:name] =~ /Plane/ }[:name]# => Plane $vehicles.find_all { |v| v[:name] =~ /Plane/ }.collect { |v| v[:name] } # => [ Plane , Sea Plane ]$vehicles.find_all { |v| v[:wheels] > 0 }.collect { |v| v[:name] } # => [ Car , Truck , Bike ]$vehicles.find_all { |v| v[:classes].include? :ground }.collect { |v| v[:name] } # => [ Car , Truck , Bike , Sea Plane ]$vehicles.find_all { |v| v[:classes].include? :air }.collect { |v| v[:name] } # => [ Plane , Helicopter , Sea Plane ] Iterating With Storage Using Inject When you are looking to collect values during iteration the inject method is the perfect one for the job. This method accepts a initialization parameter which is 0 and [] in the case below, this is then passed p $vehicles.inject(0) { |total_wheels, v| total_wheels += v[:wheels] } # => 10p $vehicles.inject([]) { |classes, v| classes + v[:classes] }.uniq # => [:ground, :water, :air] Sources This document is based on this excellent post: http://vision-media.ca/resources/ruby/ruby-enumerable-method-examples Edited and added to by Sarah Allen, blazingcloud.net Please also see reference docs: http://ruby-doc.org/core/classes/Enumerable.html