each-Select
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的更多相关文章
- 最全的ORACLE-SQL笔记
-- 首先,以超级管理员的身份登录oracle sqlplus sys/bjsxt as sysdba --然后,解除对scott用户的锁 alter user scott account unloc ...
- Matplotlib数据可视化(6):饼图与箱线图
In [1]: from matplotlib import pyplot as plt import numpy as np import matplotlib as mpl mpl.rcParam ...
- SELECT INTO 和 INSERT INTO SELECT 两种表复制语句
Insert是T-sql中常用语句,Insert INTO table(field1,field2,...) values(value1,value2,...)这种形式的在应用程序开发中必不可少.但我 ...
- select、poll、epoll之间的区别总结
select.poll.epoll之间的区别总结 05/05. 2014 select,poll,epoll都是IO多路复用的机制.I/O多路复用就通过一种机制,可以监视多个描述符,一旦某个描述符就绪 ...
- LINQ to SQL Select查询
1. 查询所有字段 using (NorthwindEntities context = new NorthwindEntities()) { var order = from n in contex ...
- ADO.NET一小记-select top 参数问题
异常处理汇总-后端系列 http://www.cnblogs.com/dunitian/p/4523006.html 最近使用ADO.NET的时候,发现select top @count xxxx 不 ...
- iosselect:一个js picker项目,在H5中实现IOS的select下拉框效果
具体文档和demo可以访问github:https://github.com/zhoushengmufc/iosselect 移动端浏览器对于select的展示样式是不一致的,ios下是类似原生的pi ...
- SQL Server中SELECT会真的阻塞SELECT吗?
在SQL Server中,我们知道一个SELECT语句执行过程中只会申请一些意向共享锁(IS) 与共享锁(S), 例如我使用SQL Profile跟踪会话86执行SELECT * FROM dbo.T ...
- (转载) Linux IO模式及 select、poll、epoll详解
注:本文是对众多博客的学习和总结,可能存在理解错误.请带着怀疑的眼光,同时如果有错误希望能指出. 同步IO和异步IO,阻塞IO和非阻塞IO分别是什么,到底有什么区别?不同的人在不同的上下文下给出的答案 ...
- 基于select的python聊天室程序
python网络编程具体参考<python select网络编程详细介绍>. 在python中,select函数是一个对底层操作系统的直接访问的接口.它用来监控sockets.files和 ...
随机推荐
- 解决SourceGrid在某些系统上无法用鼠标滚轮滚动的问题
4.40版源码,找到SourceGrid/SourceGrid/Common/CustomScrollControl.cs 这个文件 定位 CustomScrollWheel() 方法,把if条件判断 ...
- [USACO 1.5.4]checker(水题重做——位运算(lowbit的应用))
描述 检查一个如下的6 x 6的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行.每列有且只有一个,每条对角线(包括两条主对角线的所有平行线)上至多有一个棋子. 0 1 2 3 4 5 6 ------- ...
- 第二十六课:jQuery对事件对象的修复
因为原生的event对象,在不同浏览器下,有不同的属性和方法,因此需要用jQuery进行兼容. jQuery在这里分两步走,首先创建一个伪事件类jQuery.Event(jQuery里面自定义的事件类 ...
- 百度CDN公共库
百度CDN公共库 后续可以直接调用 地址:http://developer.baidu.com/wiki/index.php?title=docs/cplat/libs jQuery 加载地址: 未压 ...
- WPF--Dispatcher.BeginInvoke()方法使用不当导致UI界面卡死的原因分析
原文地址: http://www.tuicool.com/articles/F7reem http://blog.csdn.net/yl2isoft/article/details/11711833 ...
- poj2763 树链剖分(线段树)
注意这里都是把边放到线段树中,所以lca的时候,要注意如果top[x]==top[y] && x==y 的时候已经完成了. 仔细想想边和点的不同之处!!! #include<ma ...
- shell将输入的参数逆序
#! /usr/bin/ksh count=$# //总共输入参数 cmd="echo" while [[ $count -gt 0 ]] do cmd="$cmd \ ...
- 【CodeForces 620D】Professor GukiZ and Two Arrays
题 题意 两个数列,一个有n个数,另一个有m个数,让你最多交换两次两个数列的数,使得两个数列和的差的绝对值最小,求这个差的绝对值.最少交换次数.交换数对 分析 交换0次.1次可得到的最小的差可以枚举出 ...
- 人工免疫算法-python实现
AIAIndividual.py import numpy as np import ObjFunction class AIAIndividual: ''' individual of artifi ...
- Mac OS X终端下apache操作
重启apache:sudo /usr/sbin/apachectl restart 关闭apache:sudo /usr/sbin/apachectl stop 开启apache:sudo /usr/ ...