SlideShare a Scribd company logo
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

More Related Content

PPTX
Data structures and algorithms arrays
PPT
Standard Template Library (STL) in Object Oriented Programming
PDF
15 ruby arrays
PDF
16 ruby hashes
PDF
18 ruby ranges
PDF
14 ruby strings
PDF
20 ruby input output
PDF
17 ruby date time
Data structures and algorithms arrays
Standard Template Library (STL) in Object Oriented Programming
15 ruby arrays
16 ruby hashes
18 ruby ranges
14 ruby strings
20 ruby input output
17 ruby date time

Similar to 19 ruby iterators (20)

PPTX
Ruby basics ||
PPTX
Ruby basics || updated
PPTX
Ruby data types and objects
ODP
Ruby Basics by Rafiq
PDF
RubyMiniGuide-v1.0_0
PDF
RubyMiniGuide-v1.0_0
PDF
Ruby — An introduction
PDF
Ruby training day1
PPTX
7 Methods and Functional Programming
ODP
Ruby Made Simple: Blocks Plus Iterators
PDF
Ruby_Basic
PDF
Enumerables
PDF
06. Ruby Array & Hash - Ruby Core Teaching
PPT
Inside Enumerable
PDF
Blocks and loops.pptx
DOCX
Ruby Programming
KEY
Learn Ruby 2011 - Session 5 - Looking for a Rescue
PDF
New features in Ruby 2.4
PPTX
Ruby's Arrays and Hashes with examples
PDF
11 ruby methods
Ruby basics ||
Ruby basics || updated
Ruby data types and objects
Ruby Basics by Rafiq
RubyMiniGuide-v1.0_0
RubyMiniGuide-v1.0_0
Ruby — An introduction
Ruby training day1
7 Methods and Functional Programming
Ruby Made Simple: Blocks Plus Iterators
Ruby_Basic
Enumerables
06. Ruby Array & Hash - Ruby Core Teaching
Inside Enumerable
Blocks and loops.pptx
Ruby Programming
Learn Ruby 2011 - Session 5 - Looking for a Rescue
New features in Ruby 2.4
Ruby's Arrays and Hashes with examples
11 ruby methods
Ad

More from Walker Maidana (15)

PDF
13 ruby modules
PDF
12 ruby blocks
PDF
10 ruby loops
PDF
09 ruby if else
PDF
08 ruby comments
PDF
07 ruby operators
PDF
06 ruby variables
PDF
05 ruby classes
PDF
04 ruby syntax
PDF
03 ruby environment
PDF
01 index
PDF
00 ruby tutorial
PDF
02 ruby overview
PDF
21 ruby exceptions
PDF
Tutorial ruby eustaquio taq rangel
13 ruby modules
12 ruby blocks
10 ruby loops
09 ruby if else
08 ruby comments
07 ruby operators
06 ruby variables
05 ruby classes
04 ruby syntax
03 ruby environment
01 index
00 ruby tutorial
02 ruby overview
21 ruby exceptions
Tutorial ruby eustaquio taq rangel
Ad

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