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和 ...
随机推荐
- TO~亲爱的自己
你累的时候,谢绝别人的肩膀: 你扛不动的时候,拒绝别人的帮忙: 你和别人吃饭,不让别人买单你总是想我这样优秀善良, 总不给别人添麻烦,为什么总是找不到爱的人呢? 有时,爱只有在相处时才能找得到的, 是 ...
- css中的伪类和伪元素
伪类用单冒号 我们平时熟悉的a:link.a:visited.a:hover和a : active 伪元素用双冒号(为了更好的兼容我们也用单冒号) 常用的:before :after和 :fir ...
- [bzoj 1503][NOI 2004]郁闷的出纳员(平衡树)
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1503 分析: 经典的平衡树题,我用Treap做的 下面有几点注意的: 1.可能出现新加入的人的 ...
- [wikioi1553]互斥的数(数学分析+散列/数学分析+二分)
题目描述 Description 有这样的一个集合,集合中的元素个数由给定的N决定,集合的元素为N个不同的正整数,一旦集合中的两个数x,y满足y = P*x,那么就认为x,y这两个数是互斥的,现在想知 ...
- 1、面向对象以及winform的简单运用(开篇)
面向对象概述: 要学习好面向对象,我们应该从三个问题入手: 1.什么是面向对象? 2.为什么要面向对象? 3.该怎么面向对象? 面向对象,首先要有一个对象,那么对象是什么呢? 对象的定义是人们要进行研 ...
- TS
//html文件 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- HNOI2002
对于当天的营业额,你要找到之前的数和他差的绝对值最小的和.由于这个是不断在插入的,所以用伸展树来维护. http://www.lydsy.com/JudgeOnline/problem.php?id= ...
- ZOJ 3201 树形dp+背包(简单题)
#include<cstdio> #include<vector> #include<cstring> #include<iostream> using ...
- hdu3535 混合背包
分三种情况. 至少取一种 那可以直接取 或者从上一种情况来取.dp[i][k]=max(dp[i][k],dp[i-1][k-a[j].c]+a[j].v,dp[i][k-a[j].c]+a[j].v ...
- Eclipse属性文件编辑器---Properties Editor
今天在用 Eclipse 来编辑 .properties 文件时,写的中文会自动转为 Unicode 编码,完全不知道自己的中文写的是什么!! 于是查了一下,网上推荐,在Eclipse 中 安装一个 ...