前情提要
在第一天里,我们很激昂地用Ruby的类别、物件、方法,写了开赛宣言!
在第二天里,我们比较了方法与模块,比的过程中,发现模块多了包含(inclusion)与延伸(extension)。

超级比一比类别Class模块Module
父类别superclass模块Module物件Object
继承inheritance可继承不可继承(xcsjbj)
包含inclusion不可被包含可被包含*
延伸extension不可延伸可被延伸*
实例化instantiation可被实例化(instantiated)不可被实例化
所以在第三天的文章里,进一步研究module中的inclusion和extension是必须的!

Ruby经典面试题目#03
包含与延伸有什么不同?What's the Difference Between Include and Extend?

还记得我们昨天举的例子:网络图书馆(模块)有很多知识(方法)让我们取用(include),让你与我都能够突破先天(继承)的限制,变成更加聪明灵活的IT人。

module Library
def IThelp
p“I'm learning from others' IT articles on IThelp Website!”
end
end

class EveryoneLearnsRuby
def initialize(name)
@name = name
end
include Library
end

Ting = EveryoneLearnsRuby.new(“Ting”)
Ting.IThelp
You = EveryoneLearnsRuby.new(“You”)
You.IThlep
当然,使用类别(class)继承也有它的好处,

例如:在已有的功能基础上,再追加扩展本身已有功能。
(龙生龙、凤生凤;老鼠生的儿子会打洞!)

或是以相同名称的方法,重新定义,产生不同的效果。
(王老先生有块地,那王小弟长大后可以把王老先生的那块地拿去盖民宿。)

但模块(module)的include就像开外挂一样,让我们可以在这个星球上学会更多技能。

为了比较include与extend,我们把图书馆模块来稍加改写:

module Library
def IThelp
p“IThelp helps me!”
end
end

class NewbieLearnsRuby
include Library
end

NewbieLearnsRuby.new.IThelp
#IThelp helps me!

NewbieLearnsRuby.IThelp
#NoMethodError
如果我们把NewbieLearnsRuby.new.IThelp误写成NewbieLearnsRuby.IThelp,就会NoMethodError出现错误。

undefined method `IThelp' for NewbieLearnsRuby:Class(NoMethodError)
奇怪,为什么会这样呢?

我们回到改写前的图书馆例子:我先宣告(new)一个新物件You,
让「You」这个变数名字指向EveryoneLearnsRuby.new(“You”)

You = EveryoneLearnsRuby.new(“You”)
You.IThlep
所以刚刚的NewbieLearnsRuby.new.IThelp其实是以下的简化:

You = NewbieLearnsRuby.new
You.IThelp
# [NewbieLearnsRuby.new].IThelp [中括号内的变数就是You!]
这就是我们为什么不能漏掉.new的原因。

那,如果改写成extend的代码,会变成如何呢?

module Library
def IThelp
p“IThelp helps me!”
end
end

class NewbieLearnsRuby
include Library
end

class ExtendRuby
extend Library
end

NewbieLearnsRuby.new.IThelp
# IThelp helps me!

ExtendRuby.IThelp
# IThelp helps me!
由以上可知,include代表Newbie类别学Ruby时需要new一个新的物件实体,然后才能使用方法。
但extend不用,在Extend类别中使用它,可以直接把方法拿过来用(vmwork)。

ExtendRuby.IThelp
# IThelp helps me!

ExtendRuby.new.IThelp
# NoMethodError
同样的,想进一步了解为什么输入ExtendRuby.new.IThelp也是NoMethodError。接下来我们要拿关键字the difference between include and extend in ruby去请教Google大神:

Now that we know the difference between an instance method and a class method,let's cover the difference between include and extend in regards to modules.Include is for adding methods to an instance of a class and extend is for adding class methods.出处
为了抽丝剥茧这段话的含义,这里的实体方法instance method和类别方法class method将会成为我们下一篇文章的重点啰!

第三天感想
写文章真的很有趣!当我写出NewbieLearnsRuby这种名称的class,就仿佛自己像写一本武侠小说一样,尽情地创造准备开始练功的新人物、新主角。

身为新手工程师,屏幕是我们的画布~键盘上的各个中英文字、数值、符号就是我们的颜料,
享受写程序+写文章的过程,愿我们都可以在人生画布上,挥洒、创造自己的新世界!

Day03 - Ruby比一比:Module的include与extend的更多相关文章

  1. 解析UML用例图中include与extend的区别

    UML用例图有很多值得学习的地方,这里向大家简单介绍一下UML用例图中include与extend的区别,希望本文的介绍对你有所帮助. 本文和大家重点讨论一下UML用例图中include与extend ...

  2. [UML]UML系列——用例图中的各种关系(include、extend)

    用例图中的各种关系 一.参与者与用例间的关联关系 参与者与用例之间的通信,也成为关联或通信关系. 二.用例与用例之间的关系 包含关系(include) 扩展关系(extend) 包含关系 (1)  概 ...

  3. Django模板结构优化{% include %}和{% extend %}标签

    https://blog.csdn.net/xujin0/article/details/83420633

  4. Ruby模块(module)

    Ruby模块(module) 2013-04-03 16:47:09|  分类: Ruby |  标签:ruby  require  load  extend  include  |字号 订阅     ...

  5. Ruby中实现module继承

    module FooModule  def self.included base    base.extend ClassMethods  end module ClassMethods    def ...

  6. Jmeter(十九)Logic Controllers 之 Module Controller and Include Controller

    Module Controller ---模块控制器 测试计划设置“独立运行没每个线程组” 线程组2中使用Module Controller执行线程组1中的Sampler: 紧接着,将线程组1disa ...

  7. ruby 中的 module

    Module是Class的父类: >> Class.superclass => Module module 没有实例变量 module 没有new不能生成实例对象 module内可以 ...

  8. ruby里面module和class的区别

    一句话概括,就是 class可以实例化 module不可以 别的都一样 关于继承的一点区别 class是使用<作为继承的关键字,只支持单继承 module是使用include来做实例继承(实例化 ...

  9. Jmeter (二十六)逻辑控制器 之 Module Controller and Include Controller

    Module Controller ---模块控制器 测试计划设置“独立运行没每个线程组” 线程组2中使用Module Controller执行线程组1中的Sampler: 紧接着,将线程组1disa ...

随机推荐

  1. iis重写模块实现程序自动二级域名,微软提供的URL重写2.0版本适用IIS以上

    在iis7以后微软提供了url重写2.0版本,可以通过安装重写组件来实现.适用于iis7以上版本. 安装有两种方式可以选择,一是下载安装文件,二是通过“web平台安装程序”安装 1.下载安装文件 下载 ...

  2. Azure CosmosDB (13) CosmosDB数据建模

    <Windows Azure Platform 系列文章目录> 我们在使用NoSQL的时候,如Azure Cosmos DB,可以非常快速的查询非结构化,或半结构化的数据.我们需要花一些时 ...

  3. vscode setting

    { "files.autoSave": "onFocusChange", "window.openFilesInNewWindow": tr ...

  4. [蓝桥杯]PREV-13.历届试题_网络寻路

    题目描述: 代码如下: #include <stdio.h> #include <stdlib.h> #include <string.h> #define LEN ...

  5. js判断数组里是否有重复元素的方法

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/longzhoufeng/article/details/78840974 第一种方法:但是下面的这种 ...

  6. .net core Ocelot实现API网关并部署在docker中

    基于Ocelot(http://ocelot.readthedocs.io)搭建的API网关demo 软件以及系统版本:  Asp.Net Core 2.2 Ocelot 13.5.0 CentOS ...

  7. Linux基础之常用命令整理(二)

    Linux系统启动流程 bios(找到启动介质) --> mbr(找到boot loader  512B 446引导信息 64分区信息 2 标志位 ) -->grub(选择操作系统或者内核 ...

  8. 解析-ESP01模块开发Arduino物联网wifi开关模块

    本文将解析<完美图解物联网Iot实操 ESP8266>中 第五章 P177页 动手做的代码2(使用SPIFFS文件系统的代码) 首先我们先动手使用Arduino IDE编译并且上传代码,上 ...

  9. SQL JOIN 中 on 与 where 的区别

    left join : 左连接,返回左表中所有的记录以及右表中连接字段相等的记录. right join : 右连接,返回右表中所有的记录以及左表中连接字段相等的记录. inner join : 内连 ...

  10. mongodb mac

    ==> mongodb To have launchd start mongodb now and restart at login: brew services start mongodb O ...