Enumerable模块的常用方法

Enumerable模块的常用方法b enum collect obj block gt arrayenum map obj block gt array b Returnsanewa code 1 4 collect i

[b]

enum.collect {| obj | block} => array
enum.map {| obj | block} => array
[/b]
Returns a new array with the results of running block once for every element in enum.
[code]
(1..4).collect {|i| i*i } #=> [1, 4, 9, 16]
(1..4).collect { “cat” } #=> [“cat”, “cat”, “cat”, “cat”]
[/code]

[b]
enum.detect(ifnone = nil) {| obj | block } => obj or nil
enum.find(ifnone = nil) {| obj | block } => obj or nil
[/b]
Passes each entry in enum to block. Returns the first for which block is not false. If no object matches, calls ifnone and returns its result when it is specified, or returns nil.
[code]
(1..10).detect {|i| i % 5 == 0 and i % 7 == 0 } #=> nil
(1..100).detect {|i| i % 5 == 0 and i % 7 == 0 } #=> 35
[/code]

[b]
enum.inject(initial) {| memo, obj | block } => obj
enum.inject {| memo, obj | block } => obj
[/b]
Combines the elements of enum by applying the block to an accumulator value (memo) and each element in turn. At each step, memo is set to the value returned by the block. The first form lets you supply an initial value for memo. The second form uses the first element of the collection as a the initial value (and skips that element while iterating).
[code]
# Sum some numbers
(5..10).inject {|sum, n| sum + n } #=> 45
# Multiply some numbers
(5..10).inject(1) {|product, n| product * n } #=>

# find the longest word
longest = %w{ cat sheep bear }.inject do |memo,word|
memo.length > word.length ? memo : word
end
longest #=> “sheep”

# find the length of the longest word
longest = %w{ cat sheep bear }.inject(0) do |memo,word|
memo >= word.length ? memo : word.length
end
longest #=> 5
[/code]

[b]
enum.find_all {| obj | block } => array
enum.select {| obj | block } => array
[/b]
Returns an array containing all elements of enum for which block is not false.
[code]
(1..10).find_all {|i| i % 3 == 0 } #=> [3, 6, 9]
[/code]

[b]
enum.reject {| obj | block } => array
[/b]
Returns an array for all elements of enum for which block is false.
[code]
(1..10).reject {|i| i % 3 == 0 } #=> [1, 2, 4, 5, 7, 8, 10]
[/code]


























































版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/227565.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月16日 下午9:01
下一篇 2026年3月16日 下午9:02


相关推荐

  • makefile文件编写「建议收藏」

    makefile文件编写「建议收藏」makefile文件用于管理和组织代码工程的编译和链接,其不是可执行文件,其被make工具解析并完成相关动作,下面笔者将介绍makefile中常用的一些语法说明:1、文件包含:语法:include文件名作用:将其它makefile文件包含进来,组成一个更大的makefile文件,这样有利于makefile模块化编程。通常我们将一些配置选项分开成一个独立的makefile文件,这…

    2022年6月7日
    65
  • 多线程开发实战:Java实现多线程四种方式及相关方法原理

    多线程开发实战:Java实现多线程四种方式及相关方法原理本文带大家了解 Java 实现多线程的四种方法以及实现多线程的重要注意事项和难点

    2026年3月19日
    2
  • pycharm2017 激活成功教程方法

    pycharm2017 激活成功教程方法由于最近找能用的激活码稍微有点费事 所以特意整理下来为那些需要的人使用 希望对大家有所帮助 1 在激活界面的 Licenseserve 输入 http idea liyang io 或者 点击 help Register Licensesever 输入 http idea liyang io 或者 http idea imsxm com 如果上述激活网址失效 换为这个 https

    2026年3月17日
    1
  • pycharm2018打不开_pycharm indexing

    pycharm2018打不开_pycharm indexingpycharm2020无法打开,点击无反应,今天我碰到这现象,总结大体原因为2种第1种:C:\Users\ygw\AppData\Roaming\JetBrains(删除该目录即可,一般由于升级安装或安装两个不同版本会存在老旧文件影响导致)第2种:进行过激活成功教程,修改了pycharm64.exe.vmoptions配置,其中存在错误配置或配置中的指定jar…

    2022年8月29日
    3
  • css两端对齐IOS不适用 样式冲突

    css两端对齐IOS不适用 样式冲突问题 textclass explain v html info goodsExplain explain white space pre wrap 用来换行 display inline block text align justify 用来两端对齐 text align last left word break break word 文本带有换行符 没有带标签 textclass explain v html info goodsExplain

    2025年7月27日
    3
  • windows server2003 web服务器搭建

    windows server2003 web服务器搭建在控制面板中点击添加或删除程序,打开如下界面:勾选并打开应用程序服务器-Internet信息服务(IIS)-万维网服务依次点击确定后,开始下载服务,完成后打开IIS管理器然后进行如下设置在主目录下创建一个文本文档,输入内容,并保存为index.htm打开浏览器,输入IP地址,即可看到刚才输入的内容用物理机访问同理…

    2022年5月27日
    34

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号