Examples of the use of iterator iterator in Ruby _ruby topics

Source: Internet
Author: User

Iterator
Defined

A Ruby iterator is, simple A, can invoke a block of code.

    • Block is typically followed by method, and the code in the block does not necessarily execute
    • If there is a yield in method, the code in its block is executed
    • Block can receive parameters, and return value
def two_times
  yield
  yield
end
two_times {puts "Hello"}
# Hello
# Hello

def fib_up_to (max
 I1, I2 = 1.1 while
 i1 <= Max
   yield i1 i1
   , i2 = i2, I1 + i2 End-end

fib_up_to (1000 {|f| print F, ""}

# 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987

The i1 after the yield in the code above is passed into the block as parameter and assigned to the Block's argument F.
There can be multiple arguments in the block.

The common iterator
each

The simplest iterator-all it does is yield successive to its elements of the probable.

[1, 3, 5, 7, 9].each {|i| puts i} # 1 # 3 # 5 #
7
# 9

Find

A BLOCL may also return a of value to the method. The value of the last expression evaluated in the ' block is passed ' the ' method as the ' value of the ' yield.

Class Array def Find all do
  |value|
    return value if yield (value) end end

[1,3,4,7,9].find {|v| V*v >} # => 7

Collect (also known as map)

Which takes each element from the collection and passes it to the block. The results returned by the blocks are used to construct a new array

["H", "A", "L"].collect {|x| X.SUCC} # => ["I", "B", "M"]

Inject

The inject method lets your accumulate a value across the members of a collection.

[1,3,5,7].inject {|sum, element| sum + element} # =>

# sum = 1, element = 3
# sum = 4, element = 5
# s Um = 9, element = 7
# sum =

[1,3,5,6].inject {|product, element| product*element} # => 105

If inject is called with no parameter, it uses the the the collections as the initial value and starts the IT Eration with the second value.

Another easy way to write this code:

[1,3,5,7].inject (: +) # =>
[1,3,5,7]/inject (:*) # => 105

Iterator and I/O system interaction

Iterators is not only able to access data in Array and Hash, but can interact with I/O systems

f = File.Open ("testfile")
F.each do |line|
 Puts "The line is: #{line}"
End
F.close

Produces:
The line is:this are line one
The line is:this are line two
The line is:this are line three


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.