前言

这一章通过抽取一个文件中的确定的单词的项目进行讲解,主要包括了:

  1. File 的打开、阅读与关闭
  2. find_all & refuse方法的相关内容
  3. map 方法的相关内容
    这章的核心是:关于 block 具有 return value to method 的功能,以及利用这个功能实现 array 中的一些遍历的操作.

File 的打开、阅读和关闭

第一种方式:使用普通方法

打开文件:review_file = File.open("review.txt")
阅读文件lines = review_file.readlines
关闭文件review_file.close

需要关闭文件的原因:

  1. 如果同时打开了多个文件,可能会出现错误
  2. 文件阅读之后,会呈现空白,因为 already read to the end of the file, and there's nothing after that.

第二种方式:使用 with a block 的方法

File.open("review.txt") do |review_file|
lines = review_file.readlines
end

使用 with a block 的方法的优点

When the block finishes, the file is automatically closed for you.

注意事项:
由于 lines 是 block 中的 variable, 因此在 method 之外使用会出现 undefined 的错误,因此,应该在使用 lines 变量之前先定义一下:lines = []

find_all 方法 & refuse 方法

find_all 方法

格式:

relevant_lines = lines.find_all do |line|
line.include?("Truncated")
end

自我实现:

relevant_lines = []

lines.each do |line|
if line.include?("Truncated")
relevant_lines << line
end
end

refuse 方法

refuse 方法与 find_all 方法相反, 其中的「自我实现」 中的 if 变为 unless

map 方法

map 方法与 find_all 方法 & refuse 方法很 similar, 它们的不同之处是:

  • map 方法:add the block's return value itself to the new array
  • find_all 方法 & refuse 方法: use the block's return value to decide whether to copy the original element to the new array.

格式:

adjectives = reviews.map { |review| find_adjectives(review) }

自我实现:

adjectives = []

reviews.each do |review|
adjective << find_adjective(review)
end

puts adjectives

HeadFIrst Ruby 第六章总结 block return values的更多相关文章

  1. HeadFirst Ruby 第七章总结 references

    前言 这一章的内容关于 references,讲了当 Ruby 程序中可能会遇到关于 reference 与 object 之间概念混淆而导致的问题. 导言 本章从一个 astronomer 发现 s ...

  2. HeadFIrst Ruby 第七章总结 hashes

    前言 这一章节介绍了 Ruby 中 hash 这一数据类型的用法和特征. Hash 的定义 与 array 的对比 最大的不同: An array can only use integers as i ...

  3. HeadFIrst Ruby 第二章总结 methods and classes

    HeadFIrst Ruby 第二章总结 methods and classes 前言 这一章讲了如何创建自己的 class,并且讲了在用 class 创建 object 的两个要素: instanc ...

  4. 《Django By Example》第六章 中文 翻译 (个人学习,渣翻)

    书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:无他,祝大家年会都中奖!) 第六章 ...

  5. C# Language Specification 5.0 (翻译)第六章 转换

    转换使表达式可以当做一个明确的类型来加以处理.转换使得所给定类型的表达式以不同类型来处理,或使得没有某个类型的表达式获得该类型.转换可以是显式或隐式的,而这决定了是否需要显式地强制转换.比方说,从类型 ...

  6. 第六章:Reminders实验:第二部分[Learn Android Studio 汉化教程]

    Learn Android Studio 汉化教程 Reminders Lab: Part 2 This chapter covers capturing user input through the ...

  7. Flask 教程 第十六章:全文搜索

    本文翻译自The Flask Mega-Tutorial Part XVI: Full-Text Search 这是Flask Mega-Tutorial系列的第十六部分,我将在其中为Microblo ...

  8. KnockoutJS 3.X API 第六章 组件(5) 高级应用组件加载器

    无论何时使用组件绑定或自定义元素注入组件,Knockout都将使用一个或多个组件装载器获取该组件的模板和视图模型. 组件加载器的任务是异步提供任何给定组件名称的模板/视图模型对. 本节目录 默认组件加 ...

  9. Java语言程序设计(基础篇) 第六章 方法

    第六章 方法 6.2 定义方法 1.方法的定义由方法名称.参数.返回值类型以及方法体组成. 2.定义在方法头中的变量称为形式参数(formal parameter)或者简称为形参(parameter) ...

随机推荐

  1. 闭包引起的onclick不起作用

    问题描述:在html页面绑定onclick="cli()" 方法,定义在$(function( function cli(){} ))不起作用 $(function(){ func ...

  2. 20145212 罗天晨 MSF基础应用

    一.对exploit,payload,encode的理解 exploit是利用系统漏洞,相当于一个动态的过程. payload是攻击载荷,用于实现对漏洞的攻击,是实现漏洞攻击最核心的代码. encod ...

  3. Selenium Java Selection的使用

    用于向具有drop-down的选择框中输入内容 new Select(new ChromeDriver().findElement(By.cssSelector(" ..."))) ...

  4. #pragma data_seg() 共享数据// MyData段 // 进程 // DLL

    https://www.cnblogs.com/dongsheng/p/4476157.html http://www.cnblogs.com/CBDoctor/archive/2013/01/26/ ...

  5. 【Finchley】【新特性】Spring Cloud Finchley 新特性

    Finchley 正式版的发布貌似经历了相当长的时间,这次的重大发布主要带来了以下 4 项重大更新. 重大更新 1.新增 Spring Cloud Gateway 组件 Spring Cloud Ga ...

  6. ssm项目中遇到微信用户名称带有表情,插入失败问题

    ssm项目中遇到微信用户名称带有表情,插入失败问题 问题 Mysql的utf8编码最多3个字节,而Emoji表情或者某些特殊字符是4个字节. 因此会导致带有表情的昵称插入数据库时出错. 解决方法 一. ...

  7. How to Install Apache Tomcat 8.5 on CentOS 7.3

    How to Install Apache Tomcat 8.5 on CentOS 7.3 From: https://www.howtoforge.com/tutorial/how-to-inst ...

  8. LuoguP1072 Hankson的趣味题

    题目 原题链接 题解 题意即为 \[ gcd(x,a0)=a1 \\ lcm(x,b0)=b1 \\ 求x个数 \] 根据\(lcm\)的求解方式\(lcm(a,b)=a*b/gcd(a,b)\)可以 ...

  9. 深度学习课程笔记(十六)Recursive Neural Network

    深度学习课程笔记(十六)Recursive Neural Network  2018-08-07 22:47:14 This video tutorial is adopted from: Youtu ...

  10. (转)Awesome Knowledge Distillation

    Awesome Knowledge Distillation 2018-07-19 10:38:40  Reference:https://github.com/dkozlov/awesome-kno ...