each-Select

时间:2022-09-19 22:27:01

While Ruby’s each method is useful, it also comes with an awesome extended family of methods that are even more powerful!

For the next few examples, we’ll work with a slightly more complex data structure. It look like this:

friends = [
{
name: "Diego",
status: "Online"
},
{
name: "Liam",
status: "Away"
},
{
name: "Gloria",
status: "Online"
},
{
name: "Charlie",
status: "Away"
}
]

select is similar to each in that we pass it a block to run on each element in the collection, but the similarities stop there. The important difference is that select will return a new collection with only the items that the block returned true for. It sounds pretty intimidating at first, so let’s walk through an example.

We can use select to create a new Array filled with only our online friends:

online_friends = friends.select do |friend|
friend[:status] == "Online"
end

Because the block is so short, it would also work well as a one-liner:

online_friends = friends.select{|friend| friend[:status] == "Online"}

select will go through each element one at a time, starting with {name: “Diego”, status: “Online”}, passing it to the block we wrote. The block contains a single line: friend[:status] == “Online”. That line returns either true or false. If the block returns true, that specific item is added to a new Array that will be returned at the very end of select.

This table shows each step of the process:

 
each-Select
 

At the very end, select returns this Array which we save to a new online_friends variable:

[{ name: "Diego", status: "Online"}, { name: "Gloria", status: "Online"}]

each-Select的更多相关文章

  1. 最全的ORACLE-SQL笔记

    -- 首先,以超级管理员的身份登录oracle sqlplus sys/bjsxt as sysdba --然后,解除对scott用户的锁 alter user scott account unloc ...

  2. Matplotlib数据可视化(6):饼图与箱线图

    In [1]: from matplotlib import pyplot as plt import numpy as np import matplotlib as mpl mpl.rcParam ...

  3. SELECT INTO 和 INSERT INTO SELECT 两种表复制语句

    Insert是T-sql中常用语句,Insert INTO table(field1,field2,...) values(value1,value2,...)这种形式的在应用程序开发中必不可少.但我 ...

  4. select、poll、epoll之间的区别总结

    select.poll.epoll之间的区别总结 05/05. 2014 select,poll,epoll都是IO多路复用的机制.I/O多路复用就通过一种机制,可以监视多个描述符,一旦某个描述符就绪 ...

  5. LINQ to SQL Select查询

    1. 查询所有字段 using (NorthwindEntities context = new NorthwindEntities()) { var order = from n in contex ...

  6. ADO.NET一小记-select top 参数问题

    异常处理汇总-后端系列 http://www.cnblogs.com/dunitian/p/4523006.html 最近使用ADO.NET的时候,发现select top @count xxxx 不 ...

  7. iosselect:一个js picker项目,在H5中实现IOS的select下拉框效果

    具体文档和demo可以访问github:https://github.com/zhoushengmufc/iosselect 移动端浏览器对于select的展示样式是不一致的,ios下是类似原生的pi ...

  8. SQL Server中SELECT会真的阻塞SELECT吗?

    在SQL Server中,我们知道一个SELECT语句执行过程中只会申请一些意向共享锁(IS) 与共享锁(S), 例如我使用SQL Profile跟踪会话86执行SELECT * FROM dbo.T ...

  9. (转载) Linux IO模式及 select、poll、epoll详解

    注:本文是对众多博客的学习和总结,可能存在理解错误.请带着怀疑的眼光,同时如果有错误希望能指出. 同步IO和异步IO,阻塞IO和非阻塞IO分别是什么,到底有什么区别?不同的人在不同的上下文下给出的答案 ...

  10. 基于select的python聊天室程序

    python网络编程具体参考<python select网络编程详细介绍>. 在python中,select函数是一个对底层操作系统的直接访问的接口.它用来监控sockets.files和 ...

随机推荐

  1. leetcode

    Coding on LeetCode Online Judge leetcode(leetcode website) Problems algorithms 13. Roman to Integer ...

  2. Dubbo使用详解及环境搭建

    一:Dubbo简介 Dubbo是阿里巴巴提供的开源的SOA(面向服务的体系结构)服务化治理的技术框架,据说只是一部分开源的,但一些基本的需求已经可以满足的,而且可扩展性.是一种能取代PHRPC的服务调 ...

  3. Scala函数---既存类型

    语法: Type ::= InfixType ExistentialClauses ExistentialClauses ::= „forSome‟ „{‟ ExistentialDcl {semi ...

  4. 【LeetCode】- Valid Palindrome(右回文)

    [ 问题: ] Given a string, determine if it is a palindrome, considering only alphanumeric characters an ...

  5. 基于jquery的插件开发

    最近在公司做一个项目,由于后台数据太多需要分页显示,在网上找了很多插件都没有找到合适的分页插件,所有的分页插件始终达不到自己想要的效果.由于这个项目也不是很赶,就在网上查找各种资料,自己写一个基于jq ...

  6. c&num;程序设计原则

    单一职责 开闭原则:对扩展开放,对修改封闭. 方法 的职责,一个方法做的事越多,造成问题的可能性会增加. 解决的方法1:就是分拆2:写单独类

  7. Lintcode&colon; Fast Power 解题报告

    Fast Power 原题链接:http://lintcode.com/en/problem/fast-power/# Calculate the an % b where a, b and n ar ...

  8. bootstrap 问题

    less; sass: css预处理:可以直接使用.css,也可以修改.less,生成定制化的css CDN: 服务,使用这个效果会更好.theme一般不引入,jquery一般在js之前引入. 使用b ...

  9. Mongo限制规则

    文章翻译自来源:http://docs.mongodb.org/manual/reference/limits/#limit-bson-document-size 一.BSON 文档 1.BSON文档 ...

  10. 将本地jar包添加到maven中

    将需要引入的jar包拷贝到maven项目的WEB-INF/lib中 在pom.xml中配置如下: <dependency> <groupId>com.xxxxx.union&l ...

相关文章