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. ModernUI教程:第一个ModernUI应用(采用项目模板)

    在我们开始之前,请确保你已经为你的Visual2012或者2013安装了ModernUI for WPF的模板扩展: >>从Visual Studio 库 下载并安装VSIX扩展 > ...

  2. nth-child() 选择器

    给tabel tb1 td添加样式 #tb1 tr td:nth-child(2n+1) {/*奇数行样式*/ } #tb1 tr td:nth-child(2n) {/*偶数行样式*/ }

  3. Linux 容器的使用

    Linux 容器的使用 Linux 容器在 v2.6.29版本之后就加入到内核之中了, 之前虽然也听说过, 但一直没有太留心, 一直使用 KVM 来创建虚拟机. 直至最近 Docker 大出风头, 才 ...

  4. 第十一课:js操作选择器的通用函数

    1.判断文档是否是XML文档 var isXML = function(elem){ var documentElement = elem && (elem.ownerDocument ...

  5. [参考]Oracle 11g的安装

    1.Linux中安装Oracle 11g http://www.cnblogs.com/gaojun/archive/2012/11/22/2783257.html 2.Windows中安装Oracl ...

  6. 验证xml是否有效于.dtd文件

    <html> <head> <script language="javascript"> <!-- //加载解析器对象 var xmldo ...

  7. Cellphone Typing 字典树

    Cellphone Typing Time Limit: 5000ms Memory Limit: 131072KB   This problem will be judged on UVA. Ori ...

  8. python:open文件操作

    file: jim|123|1 tom|321|3 kamil|432|1 # __author__ = liukun # coding:utf-8 obj = open('file.txt','r' ...

  9. 德州扑克AI实现 TexasHoldem Poker

    参考了一下这篇文献,http://cowboyprogramming.com/2007/01/04/programming-poker-ai/ 自己用go实现了一个德州扑克AI,效果还可以. 正常和它 ...

  10. GMM算法k-means算法的比较

    1.EM算法 GMM算法是EM算法族的一个具体例子. EM算法解决的问题是:要对数据进行聚类,假定数据服从杂合的几个概率分布,分布的具体参数未知,涉及到的随机变量有两组,其中一组可观测另一组不可观测. ...