Ruby Enumerable, rubyenumerable

Source: Internet
Author: User

Ruby Enumerable, rubyenumerable

The Enumberable enumeration module can be understood as an abstraction for Traversing search and comparing sorted sets. This module provides the following two capabilities for classes through mixin:

1. You can traverse and search for enum. all? {| Object | block}: determines whether all elements in the Set meet certain conditions.

Puts [1, 2, 3, 4]. all? {| I> 0} # true

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

Enum. any? {| Object | block} determines whether at least one element meets the conditions.

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

Enum. collect {| object | block} each element passes in the block operation result and returns the enum. map Method as its alias in the form of an array.

<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}

Similar to each, the first parameter is the coordinate of each element, and the second parameter is 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: The set itself is returned after execution.

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

Find the first qualified in the Set, and no ifnone code block is returned.

<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 a microcosm element that meets the condition.

Enum. include? (Object) whether the set contains enum. partition {| item | block} is separated by whether the conditions are met

<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]

Enum. inject (initial = nil) {| memo, obj | block} when initial is not specified, after memo is specified as the first element obj of enum to iterate initial from the second element, memo is initially specified, iteration starts from the first element. The memo value is set as the running result of the block operation.


2. Compare and sort 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 is the same, but it consumes more resources.
<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"]

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.