HeadFirst Ruby 第九章总结 mixins & modules
前言
如果想要复用 method, 可用的方法是针对 Class 的 inheritance,但是, inheritance has its limitations,它的缺点有:
- 只能 inhert 一个 Class
- Class 的名称有一定的意义,怪异的 inheritance 会导致 class 中的方法多余,或者导致 collegues 的 misundertanding
在 Ruby 中, 可以使用 module 来解决这个问题.
关于 Modules & Mix-ins
Modules :
内容格式:与 Class 的格式相似
module MyModule
def first_method
puts "first method called."
end
def second_method
puts "second method called"
end
end
注意:
与 Class 不同的地方:不能进行 new 创建 instance
Modules 功能
它是一个 collection of methods,
When you mix a module into a class, it's like adding all of the module's methods to the class as instance method.
Mix-in 相关
引用格式: include
class MyClass
include MyModule
end
Mix-in 定义:
Modules that are designed to be mixed into classes 被称作 Mix-ins.
Mix-in 地位 & ancestor 方法
ancestor 方法:通过调用一个对象的 ancestor 方法,可以查看调用其方法时查找所需方法的顺序.
Mix-in 地位: Mix-in 的位置位于当前 Class 和 SuperClass 之间.
特别注意的一点: 在 Module 中 avoid using "initialize"
想達到的目的: 使用 Module 來 set a default value for a variable.
问题原因:
由于 MIx-in 地位的缘故,它的运行机制和当前 Class 的一个 SuperClass 很 Similar, 因此如果在当前 Class 使用 initialize 方法就会 override Module 中的 initialize.
解决办法:
- 使用条件语句:
def comments
if @comments
@comments
else
@comments = []
end
end - [简化] 使用“ || ”运算符, 例如p nil||[]
HeadFirst Ruby 第九章总结 mixins & modules的更多相关文章
- HeadFIrst Ruby 第二章总结 methods and classes
HeadFIrst Ruby 第二章总结 methods and classes 前言 这一章讲了如何创建自己的 class,并且讲了在用 class 创建 object 的两个要素: instanc ...
- 鸟哥的Linux私房菜——第九章
视频链接,推荐看B站 土豆网:http://www.tudou.com/programs/view/XmMDbjJHJC8 B站:http://www.bilibili.com/video/av966 ...
- CentOS6安装各种大数据软件 第九章:Hue大数据可视化工具安装和配置
相关文章链接 CentOS6安装各种大数据软件 第一章:各个软件版本介绍 CentOS6安装各种大数据软件 第二章:Linux各个软件启动命令 CentOS6安装各种大数据软件 第三章:Linux基础 ...
- 深入理解Magento-第九章-修改、扩展、重写Magento代码
(博主提示:本章应该不是原作者的第九章,仅作补充和参考) 作为一个开发者的你,肯定要修改Magento代码去适应你的业务需求,但是在很多时候我们不希望修改Magento的核心代码,这里有很多原因,例如 ...
- 精通Web Analytics 2.0 (11) 第九章: 新兴分析—社交,移动和视频
精通Web Analytics 2.0 : 用户中心科学与在线统计艺术 第九章: 新兴分析-社交,移动和视频 网络在过去几年中发生了不可思议的发展变化:从单向对话到双向对话的转变; 由视频,Ajax和 ...
- 第九章:四大组件之Broadcast Receiver
第九章:四大组件之Broadcast Receiver 一.广播的功能和特征 广播的生命周期很短,经过调用对象-->实现onReceive-->结束,整个过程就结束了.从实现的复杂度和 ...
- [Effective Java]第九章 异常
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- 第九章 C语言在嵌入式中的应用
上章回顾 编码的规范和程序版式 版权管理和申明 头文件结构和作用 程序命名 程序注释和代码布局规范 assert断言函数的应用 与0或NULL值的比较 内存的分配和释放细节,避免内存泄露 常量特性 g ...
- Laxcus大数据管理系统2.0(11)- 第九章 容错
第九章 容错 在当前,由于集群庞大的组织体系和复杂性,以及用户普遍要求低成本硬件,使得集群在运行过程中发生的错误概率,远远高于单一且性能稳定的小型机服务器,并且集群在运行过程中几乎是不允许停止的,这就 ...
随机推荐
- linux判断文件大小
第一条code ll -s | tail -n +2 | awk '$1 >= 10 {print $1,$10 "容量大于10"} $1 <= 9 {print $1 ...
- LVS群集配置
第一步:网络环境配置内网网段:10.0.0.0/24DR:10.0.0.254rs1:10.0.0.1rs2:10.0.0.2nfs:10.0.0.3 第二步:nfs和web服务搭建 nfs服务器:安 ...
- Ubuntu软件操作的相关命令
Ubuntu软件操作的相关命令 sudo apt-get update ------------------------------- 更新源 sudo apt-get install package ...
- bash 变量
本地变量: 变量赋值:name=value 变量引用:${name} , $name "":变量名会替换为其值 '':变量名不会替换为其值 查看变量: set 撤销变量:unse ...
- css-过渡
css过渡:元素从一种样式逐渐改变为另一种的效果.过渡所需的条件:1.所过渡的元素必须有css样式.2.必须有过渡时间.以下是过渡元素的属性:transition:简写属性,用于在一个属性中设置四个过 ...
- C语言动态链表数据结构
链表的操作增删改查 typedef int DATA; struct SNode { DATA data; SNode* pNext; }; SNode* g_head=NULL;//全局变量 //从 ...
- linux --- 4. 虚拟环境
一.虚拟环境的两种安装方式 1. virtualenv 虚拟环境 ①下载 virtualenv pip3 install -i https://pypi.tuna.tsinghua.edu.cn/s ...
- ant安装
- Python 必备好库 - 好工具收藏
apscheduler collections collections.OrderDict collections.defaultdict Python 标准库提供了 collections 模块.这 ...
- Linux命令3——c
cal:calender,显示月历 -j:用凯撒历(dates of julius caesar)的形式来显示月历,不分月份.1-365/366 -m:显示月历时,把星期一定为一周的开始.默认星期日为 ...