ruby 模块
来源:互联网 发布:新手网球拍淘宝 编辑:程序博客网 时间:2024/04/29 09:47
1、module 模块不能被实例化,也不能被子类化。模块是独立的,在继承体系中没有任何所谓的“模块等级”。所有的类都是模块,但并非所有的模块都是类。
模块名需用大写字母开头。定义一个模块会创建一个跟模块同名的常量,这个常量的值是一个Module对象,用于代表那个模块。
- <pre name="code" class="ruby">module Base64
- #constant
- DIGITS = "ABCDEF"
- # Method
- def self.encode
- end
- def self.decode
- end
- end
- #This is how we invoke the methods of the Base64 module
- test = Base64.encode(data)
- data = Base64.decode(text)
- str = Base64::DIGITS</pre><br>
- <br>
- <p></p>
- <pre></pre>
- <br>
- 2、模块用于混入
- <p></p>
- <p>想把一个模块混入一个类中,使用include</p>
- <p></p><pre name="code" class="ruby">class Point
- include Comparable
- end</pre><br>
- 3、可以把一个模块包含在另一个模块中<p></p>
- <p></p><pre name="code" class="ruby">module Iterable
- include Enumerable
- def each
- loop {yield self.next}
- end
- en</pre><br>
- 4、使用Object.extend方法,使模块实例方法成为接收对象的单键方法。<p></p>
- <p></p><pre name="code" class="ruby">countdown = Object.new
- #The each iterator as a singleton Method
- def countdown.each
- yield 1
- yield 2
- yield 3
- end
- countdown.extend(Enumerable)
- print countdown.sort #=> [1,2,3]</pre><br>
- <br>
- <p></p>
- <br>
0 0
- ruby 模块
- Ruby中的模块
- 使用Ruby DBI模块
- 使用Ruby DBI模块
- 使用Ruby DBI模块
- 使用Ruby DBI模块
- Ruby中的模块
- ruby入门_模块
- ruby DBI模块
- 使用Ruby DBI模块
- ruby中的模块
- ruby 类和模块
- ruby 类和模块
- Ruby模块Module
- ruby安装mysql2模块
- 使用Ruby DBI模块
- 使用Ruby DBI模块
- Ruby的OpenURI模块
- 优先队列(二)
- ruby 类和模块
- poj2488 A Knight's Journey
- Same Tree —— Leetcode
- cenos yum epel 源
- ruby 模块
- ruby IO操作
- linux下编译自己的库文件实践
- 「HD_ACM」 A+B for Input-Output Practice (II)
- linux 文件加锁,解锁,fcntl函数
- ruby define_method
- 聚类算法K-Means, K-Medoids, GMM, Spectral clustering,Ncut
- 在centos6.4 上安装apache (httpd-2.2.29.tar.gz) 和php(php-5.6.8.tar.gz)
- Android中RelativeLayout各个属性的含义