前情提要
在第一天里,我们很激昂地用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. MessageFormat.format 包含单引号引起的不可替换

    MessageFormat.format("region = '{0}'", "en");实际结果是region = {0}如果需要被替换的话,需要用双单引号 ...

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

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

  3. CentOS7 搭建 SVN 服务器

    CentOS7 搭建 SVN 服务器 介绍SVN: SVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS.CVS,它采用了分支管理系统,它的设计目标就是取代CVS.互联网上 ...

  4. .Net Core跨平台应用研究-HelloArm(串口篇)

    引言 为了验证采用dotnet core技术开发的物联网设备数据采集接入服务应用是否能在高性价比的linux嵌入式平台运行,针对dotnet core应用程序进行嵌入式linux环境的发布部署运行验证 ...

  5. 你云我云•兄弟夜谈会 第三季 企业IT架构

    你云我云•兄弟夜谈会 第三季 企业IT架构 你云我云•兄弟夜谈会 第二季 5G 你云我云•兄弟夜谈会 第一季 企业云 0. 概况 时间:2019年2月23日 22:00~23:30 主题:企业IT架构 ...

  6. 7.2 if else 语句

    7.2 if else 语句 if else语句的通用形式是: if ( expression ) statement1 else statement2 如果expression为真(非0),则执行s ...

  7. node vue

    官网 ECMAScript 6 Node.Js WebPack Vue.js Vuex Vue-loader (类比css-loader,是webpack中用于处理.vue文件的) vue-route ...

  8. UIView和CALayer区别

    (1)首先UIView可以响应用户的触摸事件,Layer不可以. (2)View中frame getter方法,bounds和center,UIView并没有做什么工作:它只是简单的各自调用它底层的C ...

  9. Python3.6.2安装pip install paramike模块报错

    问题描述: 在有几台电脑上pip install paramike报错 报错内容: Could not find a version that satisfies the requirement sq ...

  10. C# 字符串转为DateTime类型

    方法一:Convert.ToDateTime(string) string格式有要求,必须是yyyy-MM-dd hh:mm:ss ================================== ...