1.简介
2.安装
3.基本语法
4高级进阶
------继续
3.基本语法
3.10 Modules 模块
模块是将方法,类,和常量分门别类的方式。有俩大有点:
-
模块提供namespace,防止名字冲突 provide a namespace and prevent name clashes.
-
模块实现混合设施(mixin facility)
模块定义命名空间,一个沙箱,沙箱中的方法和常量你可以放心使用而不必担心被其它方法和常量干扰。
格式
module Identifier
statement1
statement2
...........
end
模块常量和类常量一样,开头用大写字母。模块定义类似,模块方法也和类方法一样来定义。
和类方式一样,通过在模块方式前置模块名加点(.)来调用;而引用常量前置模块名和俩个分号。
# Module defined in trig.rb file
module Trig
PI = 3.141592654
def Trig.sin(x)
# ..
end
def Trig.cos(x)
# ..
end
end
可以定义另外一个同名但不同功能的模块
# Module defined in moral.rb file
module Moral
VERY_BAD = 0
BAD = 1
def Moral.sin(badness)
# ...
end
end
和类方式一样,在模块中定义一种方法,指定模块名,加点,然后是方法名字。
3.10.1 require 语句
require和C/C++的include, Python 的import类似。
格式
require filename
这儿文件名,不需要带rb后缀。
$LOAD_PATH << '.'
require 'trig.rb'
require 'moral'
y = Trig.sin(Trig::PI/4)
wrongdoing = Moral.sin(Moral::VERY_BAD)
使用$LOAD_PATH << '.' 来指定当前目录为载入目录,如果你不想使用环境变量 $LOAD_PATH,那么可以使用require_relative来包含相对目录。
重要 −不同文件有同样的函数,这会导致歧义。可以使用模块名来调用适当的函数,避免歧义。
3.10.2 include 语句
可以在类中嵌入模块。
格式
include modulename
If a module is defined in a separate file, then it is required to include that file using require statement before embedding module in a class. 如果模块定义在一个分开的文件中,在嵌入模块到类之前,需要使用require 语句将那个文件include 进来。
support.rb:
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"
end
end
现在,可以将此模块include到类中:demo_include.rb
#!/usr/bin/ruby
$LOAD_PATH << '.'
require "support"
class Decade
include Week
no_of_yrs = 10
def no_of_months
puts Week::FIRST_DAY
number = 10*12
puts number
end
end
d1 = Decade.new
puts Week::FIRST_DAY
Week.weeks_in_month
Week.weeks_in_year
d1.no_of_months
运行结果:
zzl@zzl-VirtualBox:~/rubyprojects$ ruby demo_include.rb
Sunday
You have four weeks in a month
You have 52 weeks in a year
Sunday
120
3.10.3 Mixins 混合
类可以多重继承。
Ruby 不直接支持多重继承,但是Ruby 模块提供了称做混合Mixin的设施。
Mixins 提供向类添加功能的美妙控制方式,不过mixin的真正威力它的代码可以与它使用的类的代码交互。
检查以下示例代码:demo_mixin.rb
module A
def a1
puts "method a1 in module A"
end
def a2
puts "method a2 in module A"
end
end
module B
def b1
puts "method b1 in module B"
end
def b2
puts "method b2 in module B"
end
end
class Sample
include A
include B
def s1
puts "method s1 in class Sample"
end
end
samp = Sample.new
samp.a1
samp.a2
samp.b1
samp.b2
samp.s1
执行结果:
zzl@zzl-VirtualBox:~/rubyprojects$ ruby demo_mixin.rb
method a1 in module A
method a2 in module A
method b1 in module B
method b2 in module B
method s1 in class Sample
模块 A 包含 a1,a2俩个方法. 模块 B 有b1,b2方法. Sample 类 include 模块A 和模块B。Sample 类可以访问 所有四个方法, 即y, a1, a2, b1, and b2. 这样,你可以说Sample类有多重继承或混合mixin。
3.11 string 字符串
字符串对象存储和操作任意序列的单字节或多字节,典型的是人类语言。
单引号来定义字符串 (‘’).
'This is a simple Ruby string literal'
如果单引号要在字符串中,则需要转义符\’. 例如:
'Won\'t you read O\'Reilly\'s book?'
3.11.1Expression Substitution 表达式替换
表达式替换是将Ruby 表示式值使用#{}来嵌入的方式。
#!/usr/bin/ruby
x, y, z = 12, 36, 72
puts "The value of x is #{ x }."
puts "The sum of x and y is #{ x + y }."
puts "The average was #{ (x + y + z)/3 }."
结果:
The value of x is 12.
The sum of x and y is 48.
The average was 40.
3.11.2 General Delimited Strings 通用分割字符串
With general delimited strings, 使用通用分割的字符串,可以在一对匹配的任意分隔符中创建字符串。分隔符,例如 : !, (, {, < 等, 前置字符 (%). Q, q, a和 x 有特殊的意义。
%{Ruby is fun.} 等同于 "Ruby is fun."
%Q{ Ruby is fun. } 等同于 " Ruby is fun. "
%q[Ruby is fun.] 等同于单引号字符串
%x!ls! 等同于 `ls`
3.11.3 Escape 逃逸字符
下表是逃逸或不可打印字符,可以用\来表示。
Character Encoding 字符编码
Ruby 默认是 ASCII码,可以用单字节表示。 UTF-8, 是另外一种现代字符集,可以用1到4个字节表示。
你可以用变量 $KCODE 在程序开头定义字符集:
$KCODE = 'u'
3.11.4 String Built-in Methods 字符串内置方法
调用字符串方法需要字符串对象实例。以下方式创建字符串对象实例:
new [String.new(str = "")]
这将返回一个新的字符串对象,它有参数str的副本。然后,可以使用任何可用的实例方法。
#!/usr/bin/ruby
myStr = String.new("THIS IS TEST")
foo = myStr.downcase
puts "#{foo}"
执行结果:
this is test
3.11.5 String unpack Directives 字符串解包指令
举例:
"abc \0\0abc \0\0".unpack('A6Z6') #=> ["abc", "abc "]
"abc \0\0".unpack('a3a3') #=> ["abc", " \000\000"]
"abc \0abc \0".unpack('Z*Z*') #=> ["abc ", "abc "]
"aa".unpack('b8B8') #=> ["10000110", "01100001"]
"aaa".unpack('h2H2c') #=> ["16", "61", 97]
"\xfe\xff\xfe\xff".unpack('sS') #=> [-2, 65534]
"now = 20is".unpack('M*') #=> ["now is"]
"whole".unpack('xax2aX2aX1aX2a') #=> ["h", "e", "l", "l", "o"]