HeadFIrst Ruby 第六章总结 block return values
前言
这一章通过抽取一个文件中的确定的单词的项目进行讲解,主要包括了:
- File 的打开、阅读与关闭
- find_all & refuse方法的相关内容
- map 方法的相关内容
这章的核心是:关于 block 具有 return value to method 的功能,以及利用这个功能实现 array 中的一些遍历的操作.
File 的打开、阅读和关闭
第一种方式:使用普通方法
打开文件:review_file = File.open("review.txt")
阅读文件lines = review_file.readlines
关闭文件review_file.close
需要关闭文件的原因:
- 如果同时打开了多个文件,可能会出现错误
- 文件阅读之后,会呈现空白,因为 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的更多相关文章
- HeadFirst Ruby 第七章总结 references
前言 这一章的内容关于 references,讲了当 Ruby 程序中可能会遇到关于 reference 与 object 之间概念混淆而导致的问题. 导言 本章从一个 astronomer 发现 s ...
- HeadFIrst Ruby 第七章总结 hashes
前言 这一章节介绍了 Ruby 中 hash 这一数据类型的用法和特征. Hash 的定义 与 array 的对比 最大的不同: An array can only use integers as i ...
- HeadFIrst Ruby 第二章总结 methods and classes
HeadFIrst Ruby 第二章总结 methods and classes 前言 这一章讲了如何创建自己的 class,并且讲了在用 class 创建 object 的两个要素: instanc ...
- 《Django By Example》第六章 中文 翻译 (个人学习,渣翻)
书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:无他,祝大家年会都中奖!) 第六章 ...
- C# Language Specification 5.0 (翻译)第六章 转换
转换使表达式可以当做一个明确的类型来加以处理.转换使得所给定类型的表达式以不同类型来处理,或使得没有某个类型的表达式获得该类型.转换可以是显式或隐式的,而这决定了是否需要显式地强制转换.比方说,从类型 ...
- 第六章:Reminders实验:第二部分[Learn Android Studio 汉化教程]
Learn Android Studio 汉化教程 Reminders Lab: Part 2 This chapter covers capturing user input through the ...
- Flask 教程 第十六章:全文搜索
本文翻译自The Flask Mega-Tutorial Part XVI: Full-Text Search 这是Flask Mega-Tutorial系列的第十六部分,我将在其中为Microblo ...
- KnockoutJS 3.X API 第六章 组件(5) 高级应用组件加载器
无论何时使用组件绑定或自定义元素注入组件,Knockout都将使用一个或多个组件装载器获取该组件的模板和视图模型. 组件加载器的任务是异步提供任何给定组件名称的模板/视图模型对. 本节目录 默认组件加 ...
- Java语言程序设计(基础篇) 第六章 方法
第六章 方法 6.2 定义方法 1.方法的定义由方法名称.参数.返回值类型以及方法体组成. 2.定义在方法头中的变量称为形式参数(formal parameter)或者简称为形参(parameter) ...
随机推荐
- [c/c++] programming之路(6)、ASCII码,数据类型、随机数、字符转换及拼接等
一.变量 #include<stdio.h> #include<stdlib.h> void main0(){ //数据使用必须在范围内,否则产生溢出 unsigned +;/ ...
- adb驱动安装和使用报错笔记
adb驱动安装 adb驱动下载地址:https://adb.clockworkmod.com/ 安装时候选择一个容易记住的路径,这个很重要,因为adb驱动没有自动配置环境变量,所以实验时候将adb安装 ...
- Codeforces Round #427 (Div. 2) Problem D Palindromic characteristics (Codeforces 835D) - 记忆化搜索
Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th nu ...
- libcurl 设置代理,通过Fiddler可以进行抓包
转载:https://blog.csdn.net/jaryguo/article/details/53021923 转载:https://www.cnblogs.com/miantest/p/7289 ...
- upc组队赛1 流连人间的苏苏
流连人间的苏苏 题目描述 苏苏在做红尘仙的任务时,发现坐落于风景秀丽.四季如春的昆明市的云南中医学院. 没过多久,苏苏就喜欢上了这个学校.以致于苏苏忘了回涂山的时间,现在她只剩下d天的时间待在云南中医 ...
- Kibana --> Getting Started -->Building your own dashboard
https://www.elastic.co/guide/en/kibana/6.6/tutorial-build-dashboard.html Building your own dashboard ...
- Run tomcat on port 80 not 8080
How to run Tomcat on Port 80 A standard Tomcat installation starts the webserver on port 8080 – whic ...
- (转)Awesome Knowledge Distillation
Awesome Knowledge Distillation 2018-07-19 10:38:40 Reference:https://github.com/dkozlov/awesome-kno ...
- Ubuntu14.04 clang3.8 Installation Guide
Reference Installing clang 3.8 on Ubuntu 14.04.3. Ubuntu14.04 clang3.8 Installation Guide 1.add the ...
- Python 爬取 猫眼 top100 电影例子
一个Python 爬取猫眼top100的小栗子 import json import requests import re from multiprocessing import Pool #//进程 ...