Ruby's Enumerable

Source: Internet
Author: User

The Enumberable enumerable module can be understood as an abstraction to traverse the search and compare sorted collections. This module provides two capabilities for classes through Mixin:

1, you can traverse the search Enum.all? {|object| block} Determines whether all elements in the collection meet a condition

P UTS [1,2,3,4].all? {|i| i>0} #true

  puts [1,2,3,4].all? {|i| i>3} #false

enum.any? {|object| block} determines if at least one element satisfies the condition

<span style= "White-space:pre" ></span> puts [1,2,3,4].any? {|i| i>3} #true

enum.collect{|object| Block} The result of each element passing in the block is returned as an array Enum.map for its alias method

<span style= "White-space:pre" ></span>puts [1,2,3,4].collect{|i| i+1} #[2,3,4,5]

enum.each_with_index{|object,index| block}

Like each, the first parameter is each element, and the second corresponds to the coordinate of the element

<span style= "White-space:pre" ></span>puts [1,2,3,4].each_with_index{|item,index| puts "#{item}-#{index } "} #1-0 2-1 3-2 4-3 [1,2,3,4]

Note: Returns the collection itself after execution

Enum.find (Ifnone=nil) {|object| block}

Finds the first qualifying in the collection and does not return a Ifnone code block

<span style= "White-space:pre" ></span>block=proc.new () {puts "hello"}puts [1,2,3,4].find (block) {|item| ITEM>5} #hello

enum. Find_all{|i| block} enum.select{|i| block}

Returns the Epitome element that meets the criteria

enum.include? (object) Whether the collection containsenum.partition{|item| Block} by whether it is eligible to split

<span style= "White-space:pre" ></span>result=[1,2,3,4].partition () {|item| Item>1}<span style= " White-space:pre "></span>puts result[0"   #{2,3,4}<span style= "White-space:pre" ></span> Puts "=====" <span style= "White-space:pre" ></span>puts result[1]

e Num.inject (initial=nil) {|memo,obj| block} Initial unspecified when memo is enum first element obj iteration from second start when initial is specified, the memo starts with the specified value and begins the iteration from the first element. The value of memo will be set to the result of the block run


2. Can be compared and sorted enum. Max enum. Max{|a,b| block}
enum. Min enum. Min{|a,b| block}Enum.sort enum.sort{|a,b| block} enum.sort_by and Enum.sort, but more resource-intensive.
<span style= "White-space:pre" ></span>puts ["a", "ABC", "AB"].sort{|a,b| A.length<=>b.length}
<span style= "White-space:pre" ></span>#["a", "AB", "ABC"]

Ruby's Enumerable

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.