Summary of ruby iterator knowledge

Time:2020-9-24

In short: iteration refers to doing the same thing repeatedly, so iterator is used to repeat the same things many times.

Iterators are collection supported methods. Objects that store a set of data members are called collections. In ruby, arrays and hashes can be called collections.

Iterators return all elements of the collection, one after another. Here we will discuss two iterators, each and collect.

Ruby each iterator

Each iterator returns all elements of an array or hash.

grammar


collection.each do |variable|
  code
end

Code is executed for each element in the collection. Here, a collection can be an array or hash.


#!/usr/bin/ruby
 
ary = [1,2,3,4,5]
ary.each do |i|
  puts i
end

The output results of the above examples are as follows:

1
2
3
4
5

Each iterator is always associated with a block. It returns each value of the array to the block, one after the other. The value is stored in the variable I and then displayed on the screen.

Ruby collect iterator

The collect iterator returns all elements of the collection.

grammar


collection = collection.collect

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

example


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

The output results of the above examples are as follows:

1
2
3
4
5

Note: the collect method is not the correct way to copy between arrays. Here is another method called clone, which copies one array to another.

When you want to do something on each value to get a new array, you usually use the collect method. For example, the following code generates an array with a value of 10 times each value in a.


#!/usr/bin/ruby
 
a = [1,2,3,4,5]
b = a.collect{|x| 10*x}
puts b

The output results of the above examples are as follows:

10
20
30
40
50

supplement

Java needs to convert map into a container of list type to use iterators. However, ruby has iterators for map directly


sum = 0
cutcome = {"block1" => 1000, "book2" => 1000, "book3" => 4000}
cutcome.each{|item, price| sum += price}
print "sum = " + sum.to_s

You can even do this:


sum = 0
cutcome = {"block1" => 1000, "book2" => 1000, "book3" => 4000}
cutcome.each{|pair| sum += pair[1]}
print "sum = " + sum.to_s

Above is the detailed content of ruby iterator knowledge summary. Please pay attention to other related articles of developeppaer for more information about Ruby iterators!

Recommended Today

Summary of computer foundation for 2021 autumn recruitment interview database, redis

Series of articles: Summary of computer foundation in autumn 2021 interview algorithm, data structure, design pattern, Linux Summary of computer foundation in autumn 2021 interview – Java foundation, JVM, spring framework Summary of computer foundation for 2021 autumn recruitment interview database, redis Summary of computer foundation for 2021 autumn recruitment interview – operating system Summary […]