前情提要
在第一天里,我们很激昂地用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. 关于微信小程序获取二维码的踩坑记录

    1.踩坑需求:获取小程序的二维码 2.踩坑接口: https://api.weixin.qq.com/wxa/getwxacode?access_token=ACCESS_TOKEN3 踩坑代码 pu ...

  2. debian shell脚本关联

    懒得命令行一个个的输 设置,MIME类型编辑,搜索x-shellscript,默认的改成/bin/bash即可

  3. Linux下MySQL编码的修改

    默认登录mysql之后可以通过SHOW VARIABLES语句查看系统变量及其值. mysql> show variables like '%character%';   说明:以下是在Cent ...

  4. ORACLE数据库自动备份压缩的批处理脚本 rar 7z

    使用7z的版本: @echo offset filename="d:\backup\dbname_%date:~0,10%"set zipfile="d:\backup\ ...

  5. 【linux】之日志查看

    搜索日志 -n 显示行号 grep 1570xxxx -n callback.tomcat-catalina-out 显示从第多少行~多少行 sed -n '464913,465020p' callb ...

  6. kibi - join and filter data from multiple Elasticsearch indexes

    Kibi extends Kibana 4.6.4 with data intelligence features. The core feature of Kibi is the capabilit ...

  7. 7.11 animals.c 程序

    7.11 animals.c 程序 #include <stdio.h> #include <ctype.h> int main(void) { char ch; printf ...

  8. VMware与Centos系统安装

    Linux介绍 1. Linux Linux和windows一样都是操作系统,Linux是开源的.免费的.自由传播的类Unix操作系统软件. 是一个基于POSIX和UNIX的多用户.多任务.支持多线程 ...

  9. Layout-2相关代码:3列布局代码演化[一]

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  10. Web 应用程序项目 Himall.Web 已配置为使用 IIS。 无法访问 IIS 元数据库

    Web应用程序项目XXXX已配置为使用IIS.无法访问IIS元数据库.您没有足够的特权访问计算机上的IIS网站,xxxxiis 问题:Windows8下直接使用VS打开项目,出现问题:XXXX已配置为 ...