ruby methods block moudle 3
来源:互联网 发布:java 代码 log4j 编辑:程序博客网 时间:2024/05/19 18:17
学到这里感觉应该把class也放到一起再细细的学习一下,在学习class时没有把继承等ruby面向对象的特点写出来,等了解了moudle之后,继续学习吧。
1.moudle 类似class的定义,用关键字moudle定义一个moudle的语法:
module Identifier statement1 statement2 ...........end
moudle可以定义变量,可以定义方法,它将方法,类,块等有关联的东西组成一个大点模块 或者说一个组件。
2.引用moudle
module Moral VERY_BAD = 0 BAD = 1 def Moral.sin(badness) # ... endend保存为 Moral.rb
module Trig PI = 3.141592654 def Trig.sin(x) # .. end def Trig.cos(x) # .. endend
保存为trig.rb
require 'trig.rb'require 'moral'y = Trig.sin(Trig::PI/4)wrongdoing = Moral.sin(Moral::VERY_BAD)用require语句引用moudle,require中不能用相对路径
参考:http://rubyer.me/blog/689/
虽然百度了许多资料,但是我在window下,将rb文件放在D:\ruby\trig.rb 下去引用还是报错找不到相应的rb文件。。。。
只有将rb文件放在C:\Ruby200\lib\ruby\2.0.0目录下才成功过。
module Week FIRST_DAY = "Sunday" def Week.weeks_in_month puts "You have four weeks in a month" end def Week.weeks_in_year puts "You have 52 weeks in a year" endend保存为 Week.rb文件 放在ruby的C:\Ruby200\lib\ruby\2.0.0目录下 ,
运行:
require "Week"class Decadeinclude Week no_of_yrs=10 def no_of_months puts Week::FIRST_DAY number=10*12 puts number endendd1=Decade.newputs Week::FIRST_DAYWeek.weeks_in_monthWeek.weeks_in_yeard1.no_of_months这里用到了include命令,把一个moudle引入一个class里面
来个乱一点的例子吧
module A def a1 end def a2 endendmodule B def b1 end def b2 endendclass Sampleinclude Ainclude B def s1 endendsamp=Sample.newsamp.a1samp.a2samp.b1samp.b2samp.s1这算是基本了解了block method moudle class了,但是关于ruby的继承,多态都还没影呢。。。。。。
- ruby methods block moudle 3
- ruby methods block moudle 1
- ruby methods block moudle 2
- Kernel Methods in Ruby
- rhyming methods(ruby)
- [Ruby] Define abstract methods
- Ruby Block
- ruby学习笔记(三)--ruby methods
- Ruby | Block和迭代器
- Amazing ruby: block & yield
- Ruby的Block
- ruby block & proc & lambda
- 浅析Ruby中的methods,private_methods和instance_methods
- ruby中的block和迭代器
- Ruby 中的 block、proc、lambda
- ruby 中的Block,lambda,Proc
- ruby中的 Proc、block说明
- Ruby 中的 block、proc、lambda
- squid 的配置详解 (转)--SeriesIV
- hdu3630
- [求教]RewriteRule中的诡异现象
- DataTorrent 将数据分析速度从“实时”提升至“现在时”
- Ext.grid.GridPanel
- ruby methods block moudle 3
- 最大子矩阵
- 【七月限免】数据堂-75个中文领域词典系列
- Android解析xml文件的3种方式
- C# 正则表达式的使用详解
- squid 的配置详解 (转)--SeriesV
- Mysql \G 参数
- VC++中的匿名联合
- 安装VS2010的SP1补丁的办法(win7 x64)