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和 ...
随机推荐
- 仿照easy-ui并改进的表单验证
概述 easy-ui有自身的一套表单验证,扩展方便,但默认下也存在一些弱点,比如多规则验证.后台验证.远程异步验证等,这些功能要解决起来是比较吃力的.我仿照它的样式,写了一套前端表单验证的validB ...
- LinuxMint下Apache Http源码安装过程
1. 源码包下载 Apache Http安装要求必须安装APR.APR-Util.PCRE等包. Apache Http包下载地址:http://httpd.apache.org/download.c ...
- 游戏服务器端引擎--DogSE的设计
就DogSE的设计目标来说,它定位为千人左右的页游服务器,在不修改任何底层模块的情况下可以快速的写各种游戏业务.就算是新人在熟悉2~3天后也可以开始写一个游戏. 项目可以从github获得,访问地址: ...
- ThinkPHP之项目搭建
前言 在做javaweb开发时,我们创建一个项目,MyEclipse会自动为我们生成相应的目录结构,我们在此结构上才进行web开发,在使用TinkPHP框架做PHP开发时,一样,我们如何生成一个标准的 ...
- 序列化和反序列化的几种方式(JavaScriptSerializer 、XmlSerializer、DataContractSerializer)(一)
JavaScriptSerializer 类 为启用 AJAX 的应用程序提供序列化和反序列化功能. 命名空间: System.Web.Script.Serialization 程序集: Sys ...
- HDU 5113 Black And White 回溯+剪枝
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5113 Black And White Time Limit: 2000/2000 MS (Java/ ...
- nnnaaavvv
<header id="masthead" class="masthead" role="banner"> <h1 cla ...
- nginx + Lua 实现自定义WAF
文章摘自:https://github.com/unixhot/waf wget git@github.com:unixhot/waf.git
- Oracle分页查询语句
SELECT * FROM (SELECT A.*, ROWNUM RN FROM (SELECT * FROM (此处添加你要分页的表)) A WHERE ROWNUM <= 14000)WH ...
- Beta版本——冲刺计划及安排
我说的都队 031402304 陈燊 031402342 许玲玲 031402337 胡心颖 03140241 王婷婷 031402203 陈齐民 031402209 黄伟炜 031402233 郑扬 ...