导言

这一章的标题是 Ready-Made Mixes, 也就是 Ruby 已经准备好的用于 Mix-in 的 Modules, 它们是: Comparable 和 Enumerable, Comparable 常用于比较数字, Enumerable 常用于 Collection 形式的数据.本章介绍了:

  1. 如何mix in Comparable 这个 Module
  2. 如何mix in Enumerable 这个 Module

如何使用 Comparable 这个 Module

定义:

Comparable 包含的 methods 有:

  • >
  • >=
  • <
  • <=
  • ==
  • between?

注意:在 Ruby 中,比较运算符的实质是数字类型数据对象(包含 Fixnum 和 Float 对象)的 method.
比如,4 > 3 的实质是 4调用了 .> (second_number ) 的方法,传入了 3 这个参数.

基础:定义 "<=>"这个 method

定义

<=> 名称叫做 "spaceship operator",它通过比较两边的值返回 -1, 0, 1 等值.

自我实现:

module Comparable
def <(other)
(self <=> other) == -1
end

def >(other)
(self <=> other) == 1
end

def ==(other)
(self <=> other) == 0
end

def <=(other)
comparison = (self <=> other)
comparison == -1 || comparison == 0
end

def >=(other)
comparison = (self <=> other)
comparison == 1 || comparison == 0
end

def between?(first, second)
(self <=> first) >= 0 && (self <=> second) <= 0
end
end

Comparable 包含的各种方法的基础是 "<=> 方法,它们是通过这个方法来进行运算并且给出判断值的.

如何使用 Enumerable 这个 method

定义

Enumerable 这个 module 包含的常见的 methods 有:

  • find_all
  • refuse
  • map

总共包含了 50 个方法.

基础: each 方法

Enumerable 的基础是 each 这个 method ,因此需要在使用的 object 中创建 each 这个方法.

自我实现:

以分隔单词为例:

def each
string.split(" ").each do |word|
yield word
end
end

HeadFirst Ruby 第十章总结 Comparable & Enumerable的更多相关文章

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

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

  2. HeadFirst Ruby 第十五章总结 Saving and loading data

    前言 在上一章讲述了如何进行基础的操作,比如 处理 GET 请求的 get route, 再比如下载 gem 等等方面的知识.在这一章节,作者告诉我们如何储存.处理数据.整个过程分三步走: 首先,当 ...

  3. HeadFirst Ruby 第十四章总结 Web apps: Serving HTML

    前言 这一章节主要讲了如何利用 Ruby 中的 Sinatra 这个 gem 来创建一个 Web app 的具体流程,其中的要点包括了: Sinatra, a third party library ...

  4. HeadFirst Ruby 第九章总结 mixins & modules

    前言 如果想要复用 method, 可用的方法是针对 Class 的 inheritance,但是, inheritance has its limitations,它的缺点有: 只能 inhert ...

  5. HeadFirst Ruby 第七章总结 references

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

  6. HeadFIrst Ruby 第七章总结 hashes

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

  7. HeadFIrst Ruby 第六章总结 block return values

    前言 这一章通过抽取一个文件中的确定的单词的项目进行讲解,主要包括了: File 的打开.阅读与关闭 find_all & refuse方法的相关内容 map 方法的相关内容这章的核心是:关于 ...

  8. 【ruby】ruby基础知识

    Install Ruby(安装) For windows you can download Ruby from http://rubyforge.org/frs/?group_id=167 for L ...

  9. 尽量少用if else

    Michael Feathers是Object Mentor International公司的技术顾问.他的工作不仅是技术开发,他还参与对世界各地技术团队进行培训.指导等工作.他曾开发了将JUnit迁 ...

随机推荐

  1. python之udp

    import socket s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) s.bind(('127.0.0.1',8888)) while T ...

  2. Python3 tkinter基础 Button bg 按钮的背景颜色

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  3. Restful framework【第四篇】视图组件

    基本使用 -view的封装过程有空可以看一下 -ViewSetMixin # ViewSetMixin 写在前面,先找ViewSetMixin的as_view方法 # 用了ViewSetMixin , ...

  4. What are the differences between Flyweight and Object Pool patterns?

    What are the differences between Flyweight and Object Pool patterns? They differ in the way they are ...

  5. (转载)用C#实现MySQL建库及建表

    最近做一个项目,为了方便用户使用,希望可以在系统初始化的时候,自动实现MySQL数据库的建库和建表操作.在网上查了很多资料都没有找到合适的,偶尔在一个国外网站上看到了相关的内容,特把实现方法整理如下: ...

  6. Kubernetes之Controllers三

    StatefulSets StatefulSet is the workload API object used to manage stateful applications. Note: Stat ...

  7. Jenkins-Linux环境搭建

    一.安装jdk.tomcat等 1. JDK 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2 ...

  8. 解决 Boost安装:fatal error: bzlib.h: No such file or directory 问题

    参考: How to install all the boost development libraries? 解决 Boost安装:fatal error: bzlib.h: No such fil ...

  9. 用python读写excel的强大工具:openpyxl

    最近看到好几次群里有人问xlwt.wlrd的问题,怎么说呢,如果是office2007刚出来,大家用xlsx文件用不习惯,还可以理解,这都10年过去了喂,就算没有进化到office2016,还在用of ...

  10. 每日质量NPM包拖拽文件上传_react-dropzone

    一.react-dropzone 官方定义: Simple HTML5-compliant drag'n'drop zone for files built with React.js. 理解: 一个 ...