Ruby Iterator Enumerator

Source: Internet
Author: User

Iterators
An iterator is a method in which a yield statement is used, and the method of yield is called an iterator, and the iterator is not necessarily iterative, and data is transferred to the block passed to the method.

Yield passes the data to the code quickly, and the code blocks data to yield

Each method is an iterator with a yield statement

Enumerator
11 enumerators are an object of the Enumerable::enumerator, and enumerable is a module
2 need to require ' enumerator ' when using enumerator 1.8, not in 2.1
3 You can instantiate an enumerator with new, but usually use the to_enum or enum_for of the object class to return an enumerator that simply invokes the target object's each method, which means, for example, that A.to_enum returns an enumerator after execution. Each method of this enumerator invokes the each method of the A object


4 when passing a symbolic parameter and other parameters to the To_enum, or using enum_for semantically better for example
A.enum_for (: BBB,ARG1,ARG2), this: BBB is an iterator method from the A object, and the Enum_for method returns an enumerator that has a each method that invokes the iterator method for the A object above that: The BBB method, passing the remaining parameter arg1,arg2 to that iterator: BBB

For example, the string class is not enumerable, but the string class itself has three iterator methods (the method has yield, the three is a string method, not enumerable) Each_char Each_byte,each_line, We want to use a enumerable method, such as map,

s = "Hello"
S.enum_for (: Each_char). map {|c| C.SUCC}


eg

Class Test1
Include Enumerable

def hello (A, B)
Yield a+b
End
End

A = Test1.new

b =a.enum_for (: hello,1,2)

B.each do |x|
P X
End

Or

A.enum_for (: hello,1,2). Select {|x| puts X}


5 Use scenarios when a Class A, include enumerable. A's instance object has the method in enumerable, when we a = A.new, a.enum_for (: XXX) Returns an enumerator, the enumerator's each method calls this xxx method, and this xxx method is an iterator method in Class A, This allows the a object to invoke the method in enumerable and pass a quick example of the code as follows
Class Test1
Include Enumerable

def hello (A, B)
Yield a+b
End
Def each (A, B)
Yield a+b
End
End

A = Test1.new

b =a.enum_for (: hello,1,2)
B.select do |x|
P X
End


6 should also be the same in 1.9,2.1, do not need to display the call To_enum or Enum_for, when the built-in iterator is called without a code block (the built-in iterator refers to the Times,upto,downto, Step.each and enumerable related methods), refers to A.enum_for (: each) This sentence executes but does not pass the code fast, does not pass the code quickly the word automatically returns an enumerator
A.each This will return an enumerator

According to 6, the above example code B =a.enum_for (: hello,1,2) This line is omitted. Direct
A.each do |x|
P X
End

The premise is that a class must have a each iterator method

Class Test1
Include Enumerable


def each
Yield 3
End
End

A = Test1.new

A.select do |x|
P X
End

Some of the built-in classes themselves implement each method, only can do according to 6 said, if we customize a class, do not implement each method, can not be omitted. enum_for

Ruby Iterator Enumerator

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.