SlideShare ist ein Scribd-Unternehmen logo
1 von 2
Downloaden Sie, um offline zu lesen
RUBY ITERATORS - EACH AND COLLECT
http://www.tutorialspoint.com/ruby/ruby_iterators.htm                                             Copyright © tutorialspoint.com



Iterators are nothing but methods supported by collections. Objects that store a group of data members are called
collections. In Ruby, arrays and hashes can be termed collections.

Iterators return all the elements of a collection, one after the other. We will be discussing two iterators here, each and
collect. Let's look at these in detail.

Ruby each Iterator:

The each iterator returns all the elements of an array or a hash.

Syntax:

 collection.each do |variable|
    code
 end


Executes code for each element in collection. Here collection could be an array or a ruby hash.

Example:

 #!/usr/bin/ruby

 ary = [1,2,3,4,5]
 ary.each do |i|
    puts i
 end


This will produce following result:

 1
 2
 3
 4
 5


You always associate the each iterator with a block. It returns each value of the array, one by one, to the block. The
value is stored in the variable i and then displayed on the screen.

Ruby collect Iterator:

The collect iterator returns all the elements of a collection.

Syntax:

 collection = collection.collect


The collect method need not always be associated with a block. The collect method returns the entire collection,
regardless of whether it is an array or a hash.

Example:

 #!/usr/bin/ruby
a = [1,2,3,4,5]
 b = Array.new
 b = a.collect
 puts b


This will produce following result:

 1
 2
 3
 4
 5


NOTE: The collect method is not the right way to do copying between arrays. There is another method called a clone
which should be used to copy one array into another array.

You normally use the collect method when you want to do something with each of the values to get the new array. For
example, this code produces an array b containing 10 times each value in a.

 #!/usr/bin/ruby

 a = [1,2,3,4,5]
 b = a.collect{|x| 10*x}
 puts b


This will produce following result:

 10
 20
 30
 40
 50

Weitere ähnliche Inhalte

Ähnlich wie 19 ruby iterators

Ruby basics ||
Ruby basics ||Ruby basics ||
Ruby basics ||datt30
 
Lecture 7- Iterator and for loop over arrays
Lecture 7- Iterator and for loop over arraysLecture 7- Iterator and for loop over arrays
Lecture 7- Iterator and for loop over arraysSyed Afaq Shah MACS CP
 
Ruby basics || updated
Ruby basics || updatedRuby basics || updated
Ruby basics || updateddatt30
 
Ruby — An introduction
Ruby — An introductionRuby — An introduction
Ruby — An introductionGonçalo Silva
 
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdfGetting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdfinfo309708
 
(E Book) Asp .Net Tips, Tutorials And Code
(E Book) Asp .Net Tips,  Tutorials And Code(E Book) Asp .Net Tips,  Tutorials And Code
(E Book) Asp .Net Tips, Tutorials And Codesyedjee
 
Java Unit 2 (Part 2)
Java Unit 2 (Part 2)Java Unit 2 (Part 2)
Java Unit 2 (Part 2)SURBHI SAROHA
 
Collection framework (completenotes) zeeshan
Collection framework (completenotes) zeeshanCollection framework (completenotes) zeeshan
Collection framework (completenotes) zeeshanZeeshan Khan
 
(6) collections algorithms
(6) collections algorithms(6) collections algorithms
(6) collections algorithmsNico Ludwig
 
Ruby programming introduction
Ruby programming introductionRuby programming introduction
Ruby programming introductionASIT Education
 
Inside Enumerable
Inside EnumerableInside Enumerable
Inside EnumerableMike Bowler
 
Functional Smalltalk
Functional SmalltalkFunctional Smalltalk
Functional SmalltalkESUG
 
Ruby on rails tips
Ruby  on rails tipsRuby  on rails tips
Ruby on rails tipsBinBin He
 

Ähnlich wie 19 ruby iterators (20)

Ruby basics ||
Ruby basics ||Ruby basics ||
Ruby basics ||
 
Lecture 7- Iterator and for loop over arrays
Lecture 7- Iterator and for loop over arraysLecture 7- Iterator and for loop over arrays
Lecture 7- Iterator and for loop over arrays
 
Enumerable
EnumerableEnumerable
Enumerable
 
Ruby basics || updated
Ruby basics || updatedRuby basics || updated
Ruby basics || updated
 
Ruby — An introduction
Ruby — An introductionRuby — An introduction
Ruby — An introduction
 
Lists
ListsLists
Lists
 
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdfGetting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
 
Java collections notes
Java collections notesJava collections notes
Java collections notes
 
11 ruby methods
11 ruby methods11 ruby methods
11 ruby methods
 
(E Book) Asp .Net Tips, Tutorials And Code
(E Book) Asp .Net Tips,  Tutorials And Code(E Book) Asp .Net Tips,  Tutorials And Code
(E Book) Asp .Net Tips, Tutorials And Code
 
Intro to Rails 4
Intro to Rails 4Intro to Rails 4
Intro to Rails 4
 
Enumerables
EnumerablesEnumerables
Enumerables
 
Java Unit 2 (Part 2)
Java Unit 2 (Part 2)Java Unit 2 (Part 2)
Java Unit 2 (Part 2)
 
Scala collection
Scala collectionScala collection
Scala collection
 
Collection framework (completenotes) zeeshan
Collection framework (completenotes) zeeshanCollection framework (completenotes) zeeshan
Collection framework (completenotes) zeeshan
 
(6) collections algorithms
(6) collections algorithms(6) collections algorithms
(6) collections algorithms
 
Ruby programming introduction
Ruby programming introductionRuby programming introduction
Ruby programming introduction
 
Inside Enumerable
Inside EnumerableInside Enumerable
Inside Enumerable
 
Functional Smalltalk
Functional SmalltalkFunctional Smalltalk
Functional Smalltalk
 
Ruby on rails tips
Ruby  on rails tipsRuby  on rails tips
Ruby on rails tips
 

Mehr von Walker Maidana (15)

13 ruby modules
13 ruby modules13 ruby modules
13 ruby modules
 
12 ruby blocks
12 ruby blocks12 ruby blocks
12 ruby blocks
 
10 ruby loops
10 ruby loops10 ruby loops
10 ruby loops
 
09 ruby if else
09 ruby if else09 ruby if else
09 ruby if else
 
08 ruby comments
08 ruby comments08 ruby comments
08 ruby comments
 
07 ruby operators
07 ruby operators07 ruby operators
07 ruby operators
 
06 ruby variables
06 ruby variables06 ruby variables
06 ruby variables
 
05 ruby classes
05 ruby classes05 ruby classes
05 ruby classes
 
04 ruby syntax
04 ruby syntax04 ruby syntax
04 ruby syntax
 
03 ruby environment
03 ruby environment03 ruby environment
03 ruby environment
 
01 index
01 index01 index
01 index
 
00 ruby tutorial
00 ruby tutorial00 ruby tutorial
00 ruby tutorial
 
02 ruby overview
02 ruby overview02 ruby overview
02 ruby overview
 
21 ruby exceptions
21 ruby exceptions21 ruby exceptions
21 ruby exceptions
 
Tutorial ruby eustaquio taq rangel
Tutorial ruby   eustaquio taq rangelTutorial ruby   eustaquio taq rangel
Tutorial ruby eustaquio taq rangel
 

19 ruby iterators

  • 1. RUBY ITERATORS - EACH AND COLLECT http://www.tutorialspoint.com/ruby/ruby_iterators.htm Copyright © tutorialspoint.com Iterators are nothing but methods supported by collections. Objects that store a group of data members are called collections. In Ruby, arrays and hashes can be termed collections. Iterators return all the elements of a collection, one after the other. We will be discussing two iterators here, each and collect. Let's look at these in detail. Ruby each Iterator: The each iterator returns all the elements of an array or a hash. Syntax: collection.each do |variable| code end Executes code for each element in collection. Here collection could be an array or a ruby hash. Example: #!/usr/bin/ruby ary = [1,2,3,4,5] ary.each do |i| puts i end This will produce following result: 1 2 3 4 5 You always associate the each iterator with a block. It returns each value of the array, one by one, to the block. The value is stored in the variable i and then displayed on the screen. Ruby collect Iterator: The collect iterator returns all the elements of a collection. Syntax: collection = collection.collect The collect method need not always be associated with a block. The collect method returns the entire collection, regardless of whether it is an array or a hash. Example: #!/usr/bin/ruby
  • 2. a = [1,2,3,4,5] b = Array.new b = a.collect puts b This will produce following result: 1 2 3 4 5 NOTE: The collect method is not the right way to do copying between arrays. There is another method called a clone which should be used to copy one array into another array. You normally use the collect method when you want to do something with each of the values to get the new array. For example, this code produces an array b containing 10 times each value in a. #!/usr/bin/ruby a = [1,2,3,4,5] b = a.collect{|x| 10*x} puts b This will produce following result: 10 20 30 40 50