ruby基础知识

模块

模块是ruby的特色功能之一。如果说类是事物的实体以及行为,那么模块表现的

就是事物的行为部分,模块和类有以下两点不同:

  1. 模块不能拥有实例
  2. 模块不能被继承

模块的使用方法

module Mymodule
#共同的方法
end class Myclass1
include Mymodule
#Myclass1独有的方法
end class Myclass2
include Mymodule
#Myclass2独有的方法
end

使用模块可以灵活的解决下面的问题

  1. 虽然两个类拥有相同的功能,但是不希望他们作为相同的种类来考虑
  2. Ruby不支持父类多重继承。

ruby中模块会提供独立的命名空间,一个模块的方法、变量和另一个模块的方法、

变量是不同的。

模块函数

使用模块名.方法名的形式来调用模块中方法,这样的方法称为模块函数。
#检查文件是否存在
p FileTest.exist?("/user/bin/ruby") #false
#圆周率常量
p Math::PI #3.141592653589793

使用include方法把模块内的方法名、常量名合并到当前的命名空间中

include Math
p PI #3.141592653589793
p sqrt 2 #1.4142135623730951

创建模块

module 模块名
模块定义
end

模块的名字必须大写。eg:

module Helloworld
Version="1.0"
def hello(name)
puts "hello,#{name}."
end
#指定模块函数
module_function :hello
end Helloworld.hello("ruby") #hello,ruby.
p Helloworld::Version #"1.0" include Helloworld #包含模块 p Version #"1.0" hello "ruby" #hello,ruby

和类一样,在模块中定义的常量可以用模块名访问

p Helloworld::Version  #"1.0"

方法调用

如果只定义了方法,可以在模块内调用和包含该模块的语句中使用,但是不能以

“模块.方法名”调用,如果想要这样使用,可以这样在模块中使用 module_function

方法。

 module_function :hello

以“模块.方法名”调用,方法中的self指向模块对象,如果将模块包含今类中

,则该方法变成了类的实例方法,self指向类的实例。

module Helloworld
Version="1.0"
def hello
self
end
#指定模块函数
module_function :hello
end p Helloworld.hello #Helloworld
module Helloworld
Version="1.0"
def hello
self
end
end class Foo
include Helloworld
def aaa
puts "I am aaa"
end
end foo=Foo.new()
p foo.hello # #<Foo:0x0055c23667a338>

想要知道类是否包含某个模块,可以使用include?方法

module Helloworld
Version="1.0"
def hello
self
end
end class Foo
include Helloworld
def aaa
puts "I am aaa"
end
end p Foo.include? Helloworld #true

类的实例在调用方法的时候,会按照类、包含的模块、父类的顺序查找方法。

被包含的模块相当于类的虚拟父类。

使用ancestors方法和superclass方法调查类的继承关系

module Helloworld
Version="1.0"
def hello
self
end
end class Foo
include Helloworld
def aaa
puts "I am aaa"
end
end p Foo.superclass #Object
p Foo.ancestors #[Foo, Helloworld, Object, Kernel, BasicObject] p Object.ancestors #[Object, Kernel, BasicObject]
p Object.superclass #BasicObject p BasicObject.superclass #nil
p BasicObject.ancestors #[BasicObject] p Class.superclass #Module
p Class.ancestors #[Class, Module, Object, Kernel, BasicObject] p Module.superclass #Object
p Module.ancestors #[Module, Object, Kernel, BasicObject]

我们看出Object的父类是BasicObject.

Class的父类是Module,Module的父类是Object.

包含模块时查找方法的顺序

  1. 原类中方法优先级高于包含模块的
  2. 类中包含多个模块,优先使用最后包含的模块
  3. 多个模块,模块中包含模块,优先使用最后一个模块和模块中包含的模块。
  4. 相同模块第二次包含会被省略。

可以使用ancestors查看方法的顺序。

extend方法

Obejct#extend的方法用来给对象扩展功能

module Helloworld
Version="1.0"
def hello
self
end
end str="ruby"
str.extend Helloworld
p str.hello #"ruby"

ruby中的类方法:Class类的实例方法,类对象的单例方法。使用extend

方法给类添加类方法。

eg:include给对象添加实例方法,extend给类添加类方法。

module Instance_Method
Version="1.0"
def hello
puts "hello"
end
end
module Class_Method
def say
"Ruby"
end
end class Hello
include Instance_Method
extend Class_Method
end p Hello.say #"Ruby"
Hello.new.hello #hello

最新文章

  1. [vivado系列]Zynq开发常用文档
  2. ubuntu 14.04 对exfat的支持
  3. 使用yum来安装或卸载CentOS图形界面包
  4. 配置maven仓库
  5. maven 建立ssh项目
  6. Android 打包签名 从生成keystore到完成签名 -- 转
  7. 266. Palindrome Permutation
  8. JS禁止/启用滚动条
  9. select_related
  10. 玩转web之JQuery(二)---改变表单和input的可编辑状态(封装的js)
  11. 字符串匹配算法之KMP
  12. CentOS6.6 部署Apache+Svn
  13. #2019-2020-4 实验二面向对象程序设计《Java开发环境的熟悉》实验报告
  14. 记录一下对swiper4.x.js在H5单页中的滑动优化
  15. mysql无法远程连接
  16. Python - 内置函数 选例
  17. Django的form组件
  18. php保留两位小数的3种方法
  19. 关于java项目中的XML文件
  20. HDU5012:Dice(bfs模板)

热门文章

  1. Windows Service 2016 Datacenter\Stand\Embedded激活方法
  2. MySQL数据库高级二:索引优化
  3. 自动化kolla-ansible部署ubuntu20.04+openstack-victoria之镜像上传-11
  4. 【C】EM卡韦根32/24格式卡号转换的实现
  5. 模拟退火算法(1)Python 实现
  6. 基于MVC框架的JavaWeb网站开发demo项目(JSP+Servlet+JavaBean)
  7. 【cypress】5. 测试本地web应用
  8. NetCore去注册Eureka
  9. vuex 引用方法
  10. 技术面试问题汇总第002篇:猎豹移动反病毒工程师part2