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:

 
 

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. Angularjs 异步模块加载项目模板

    ng-lazy-module-seed(Angularjs 异步模块加载项目模板) 相信做过SPA项目的朋友都遇到过这个问题:页面初始化时需要加载文件太大或太多了,许多文件加载后很可能不会运行到,这是 ...

  2. deerlet-redis-client添加集群支持,邀请各路大神和菜鸟加入。

    引言 经过几周的修改,deerlet已经添加了对于redis集群的支持,策略与memcached客户端一样,采用一致性Hash.不过目前Hash的算法取自Java自带的String类型的HashCod ...

  3. 第三十四课:jQuery Deferred详解2

    上一课主要分析了jQuery1.51版本的jQuery Deferred.在jQuery1.6中,jQuery Deferred添加了两个方法,always,pipe. always用来添加回调,无论 ...

  4. 笔记:js的replace函数

    replace函数 js的replace函数与c#的有一个不同,js的只替换第一个字符 例如,var a=',1,2' var b=a.replace(',','') 结果b='1,2'(ps:这是一 ...

  5. 将定时任务cron 解析成中文

    在使用定时器 quartz 时,其中的cron 表达式,老板表示作为开发的你能看懂外,其他的非开发同事可能看不懂,要用一个他们能看懂的方式表达出来. 还好我们的项目要求的表达式不是特别的麻烦,所以就写 ...

  6. 【CodeForces 557B】Pasha and Tea

    题 题意 总共有 w 克蛋糕,2n 个盘子,第 i 个盘子容量为 ai ,n 个女孩和 n 个男孩,男孩得到的是女孩得到的蛋糕的两倍,求他们得到蛋糕的最大值. 分析 把盘子从小到大排序,然后 女生得到 ...

  7. BZOJ-1934 Vote 善意的投票 最大流+建图

    1934: [Shoi2007]Vote 善意的投票 Time Limit: 1 Sec Memory Limit: 64 MB Submit: 1551 Solved: 951 [Submit][S ...

  8. mysql 中如何查找相同的数据

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAbcAAAEYCAIAAABQvy+HAAAAA3NCSVQICAjb4U/gAAAgAElEQVR4Xu

  9. IOS基础之 (十二) Block

    一 定义 Block封装了一段代码,可以在任何时候执行. Block可以作为函数参数或者函数的返回值,而其本身又可以带输入参数或返回值. 二 使用 1. 定义函数指针,然后在实现. #import & ...

  10. POJ2309BST(树状数组)

    BST Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9182   Accepted: 5613 Description C ...