Ruby中 Include, Extend, Import, Require 的使用区别
Include
如下例当你Include一个模块到某个类时, 相当于把模块中定义的方法插入到类中。它允许使用 mixin。它用来 DRY 你的代码, 避免重复。例如, 当你有多个类时, 需要相同的函数时, 可以把函数定义到module中, 进行include。
下例假设模块Log和类TestClass在相同的.rb文件。如果它们存在于多个文件, 则需要使用 load 或 require 导入文件。
module Log def class_type "This class is of type: #{self.class}" end end class TestClass include Log end tc = TestClass.new.class_type puts tc #This class is of type: TestClass
Extend
当你使用Extend来替换 Include 的时候, 你会添加模块里的方法为类方法, 而不是实例方法。详细请看例子:
module Log def class_type "This class is of type: #{self.class}" end end class TestClass extend Log # ... end tc = TestClass.class_type puts tc # This class is of type: TestClass
当你在类中使用 Extend 来代替 Include, 如果你实例化 TestClass 并调用 class_type方法时,你将会得到 NoMethodError。再一次强调, 使用 Extend 时方法是类方法。
Require
Require 方法允许你载入一个库并且会阻止你加载多次。当你使用 require 重复加载同一个library时,require方法 将会返回 false。当你要载入的库在不同的文件时才能使用 require 方法。下例将演示 require 的使用方式。
文件 test_library.rb 和 test_require.rb 在同一个目录下。
# test_library.rb puts " load this libary "
# test_require.rb puts (require './test_library') puts (require './test_library') puts (require './test_library') # 结果为 # load this libary # true # false # false
Load
Load 方法基本和 require 方法功能一致,但它不会跟踪要导入的库是否已被加载。因此当重复使用 load 方法时,也会载入多次。大部分情况你都会使用 require 来代替 load。但当你需要每次都要加载时候你才会使用 load, 例如模块的状态会频繁地变化, 你会使用 load 进行加载,获取最新的状态。
puts load "./test_library.rb" #在这里不能省略 .rb, require可以省略 puts load "./test_library.rb" puts load "./test_library.rb" #结果 # load this libary #true # load this libary #true # load this libary #true
Ruby中 Include, Extend, Import, Require 的使用区别的更多相关文章
- Objective-C中 #include 和 #import 的区别
由于 Objective-C 兼容 C 语言,所以导入文件时, 可以使用 #include,也可以使用 #import (Objective-C 新增的) 如: #include <stdio. ...
- ruby中的extend 和 include
include include是把module中定义的instance_method给mixin,然后当做类的实例方法使用(是因为module本身不能使用module的实例方法),给类进行实例化一个对 ...
- freemarker中include与import的区别
在inc1.ftl与inc2.ftl中的内容分别是: <#assign username="刘德华">与<#assign username="张学友&q ...
- FreeMarker中<#include>和<#import>标签的区别
在使用freemarker作为前端页面模板的应用中,会有很多的freemarker模板页面,这些ftl会在不同的页面中重复使用,一是为了简化布局的管理,二是可以重复使用一些代码. 在freemarke ...
- ruby中实例变量、类变量等等的区别和联系
ruby的变量有局部变量,全局变量,实例变量,类变量,常量. 1.局部变量 局部变量以一个小写字母开头或下划线开头 局部变量有局部作用域限制(比如一个block内),它的作用域起始于声明处,结束于该声 ...
- Ruby中puts,print,p的区别
如果字符串的行尾没有包含换行符,puts就会添加一个,但print不会: print会精确打印内容并让光标留在末尾(在某些系统平台,在程序输出的末尾会自动换行): p会输出一个审查字符串,它通常会包含 ...
- Ruby中print、p、puts的区别
三个方法的作用都是将一个字符串打印到控制台 比较项目 puts print p 换行符 末尾添加换行符 末尾不加换行符 末尾添加换行符 非字符串对象的输出 调用该对象的to_s方法 ...
- JavaScript ES6中export、import与export default的用法和区别
前言 相信很多人都使用过export.export default.import,然而它们到底有什么区别呢? 在看他们之间的区别之前,我们先来看看它们的用法. ES6 import和export的用法 ...
- Ruby中的include
Ruby中的include语句应注意以下两个问题: 1.include与文件无关.C语言中,#include预处理指令在编译期将一个文件的内容插入到另一个文件中.Ruby语句只是简单地产生一个指向指定 ...
随机推荐
- 字符串转数字_atoi_stringstream
一.#include <cstdlib> 字符串转换到整型数,函数原型:int atoi(const char *nptr) 注意事项:有符号整型,能转换的最大字符串是:"214 ...
- User interface
Styles and Themes value/style <style name="CodeFont" parent="@android:style/TextAp ...
- json字符串转泛型集合对象
Dictionary<string, object> jd = js.Deserialize<Dictionary<string, object>>(item); ...
- iOS知识总结
mindNote文件下载地址 : 知识总结.zip
- [css 揭秘]-css coding tips
css 揭秘之css coding tips demo(1) html 代码: <body> <section> <div class="demo1" ...
- Mac mySql ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)的解决办法
我的环境:Mac 10.11.6 ,mysql 5.7.14 . mac mySql 报错ERROR 2002 (HY000): Can't connect to local MySQL serv ...
- 使用Jmeter监测服务器cpu、内存等性能
jmeter中可以监控服务器的CPU和内存使用情况,但是需要安装一些插件还需要在被监测服务器上开启服务. 1.下载JMeterPlugins-Standard-1.4.0.zip插件.下载后将JMet ...
- Javascript基础知识总结一
Javascript基础知识总结一 <!DOCTYPE html> <html> <head lang="en"> <meta chars ...
- 让div支持placeholder属性/模拟输入框的placeholder属性
实现方式:css div:empty:before{ content: attr(placeholder); color:#bbb;}div:focus:before{ content:none; }
- 移动应用抓包调试利器Charles
转载:http://www.jianshu.com/p/68684780c1b0 一.Charles是什么? Charles是在 Mac或Windows下常用的http协议网络包截取工具,是一款屌的不 ...