1.简介
2.安装
3.基本语法
4高级进阶
------继续
3.基本语法
3.8 Methods 方法
Ruby的方法与其它语言的函数类似。
方法名字小写开始。
方法应该在调用前定义,否则会导致异常。
格式
def method_name [( [arg [= default]]...[, * arg [, &expr ]])] expr.. end
举例:
def method_name expr.. end 或
def method_name (var1, var2) expr.. end 可以提供参数缺省值
def method_name (var1 = value1, var2 = value2) expr.. end 调用无参方法,直接用方法名:
method_name 调用有参方法:
method_name 25, 30 要注意,记住参数个数。
def test(a1 = "Ruby", a2 = "Perl") puts "The programming language is #{a1}" puts "The programming language is #{a2}" end test "C", "C++" test
结果:
The programming language is C The programming language is C++ The programming language is Ruby The programming language is Perl
3.8.1 方法返回值
Ruby 每个方法都默认返回一个值。返回的是最后一个语句的值。
def test i = 100 j = 10 k = 0 end
上例,返回最后一句的变量k 。
3.8.2 return 语句
Return 用来在方法中哦返回一个或多个值。
Syntax
return [expr[`,' expr...]] 俩个或以上返回值,数组保存返回值;如果无返回表达式,则返回nil.
例子:
return 或 return 12 或 return 1,2,3
demo_return.rb:
def test i = 100 j = 200 k = 300 return i, j, k end var = test puts var
结果:
zzl@zzl-VirtualBox:~/rubyprojects$ ruby demo_return.rb 100 200 300
3.8.3 方法参数个数是变量
Ruby 运行定义变参数个数方法。demo_varPar.rb
def sample (*test) puts "The number of parameters is #{test.length}" for i in 0...test.length puts "The parameters are #{test[i]}" end end sample "Zara", "6", "F" sample "Mac", "36", "M", "MCA"
这个参数是个变量,可以接受任何变量个数。参数前有*。 这个变参,类似C/C++的指针变量。
zzl@zzl-VirtualBox:~/rubyprojects$ ruby demo_varPar.rb
The number of parameters is 3
The parameters are Zara
The parameters are 6
The parameters are F
The number of parameters is 4
The parameters are Mac
The parameters are 36
The parameters are M
The parameters are MCA
3.8.4 类方法
类外面定义的方法,默认为private. 类内定义的方法,默认为public.
这些默认的private/public 属性, 可以被模块的public/private 属性修改。
要访问类的方法,首先要实例化类。然后使用对象,可以访问类的任何成员。
Ruby提供了不必实例化类就可以访问类方法。下面具体来看。
class Accounts def reading_charge end def Accounts.return_date end end
return_date 是如何定义的?它在类名后,通过点(.)连接。你可以直接访问类方法如下:
Accounts.return_date访问 访问这种类方法,不必创建类对象。
3.8.5 alias 语句
alias 给方法或全局变量起别名。Aliases 不能在方法主体内定义。方法的别名可以保持方法的当前定义,即便方法被overriden .c
给数字全局变量起别名是禁止的。重写内置全局变量可能导致严重问题。
格式
alias method-name method-name alias global-variable-name global-variable-name 举例
alias foo bar alias $MATCH $& 上面命令中, foo 是 bar的别名, $MATCH 是 $&的别名。
3.8.6 undef 语句
undef 撤消对方法的定义。undef 不能出现在方法主体内。
格式
undef method-name
举例:取消名为bar方法,
undef bar
3.9 Block 块
Block 概念.
-
由一堆代码组成.
-
块有名字
-
代码在大括号内 ({}).
-
块是由同块名相同的函数来调用。
-
使用yield 语句来调用快。
格式
block_name { statement1 statement2 .......... } 下面讲如何用一个简单的yield语句来调用块. 另外iayong带参yield 语句调用块。
3.9.1 yield 语句
demo_yield.rb
def test puts "You are in the method" yield puts "You are again back to the method" yield end test {puts "You are in the block"}
执行结果:
zzl@zzl-VirtualBox:~/rubyprojects$ ruby demo_yield.rb You are in the method You are in the block You are again back to the method You are in the block 可以给 yield 传参。 demo_yieldpar.rb
def test yield 5 puts "You are in the method test" yield 100 end test {|i| puts "You are in the block #{i}"}
结果:
zzl@zzl-VirtualBox:~/rubyprojects$ ruby demo_yieldpar.rb You are in the block 5 You are in the method test You are in the block 100 这儿,yield 后跟着参数。你甚至可以传多个参数。 在{}块内,在俩个垂直线内放一个变量来解释参数。
查看以下语句:
test {|i| puts "You are in the block #{i}"}
如果要传俩个参数,则
yield a, b
对于调用方式:
test {|a, b| statement}
3.9.2 Blocks and Methods 块和方法
用yield语句,从一个和块同名的方法中调用块。
def test yield end test{ puts "Hello world"}
这是最简单的实现块的例子。用yield 语句调用test 块。T
不过如果方法的参数前有&符合,那么你可以把一个块传给这个方法,块会被赋值给最后的参数。如果参数中* 和 & 符号,&靠后。
def test(&block) block.call end test { puts "Hello World!"}
执行结果:
zzl@zzl-VirtualBox:~/rubyprojects$ ruby demo_blockPar.rb Hello World!
3.9.3 BEGIN and END Blocks
Ruby 代码文件都可以声明代码块,当文件载入时运行(BEGIN 块),程序执行完后,再执行END 块。
一个程序可以有多个BEGIN 和END块。BEGIN块顺序执行,END块反序执行。
demo_beginend2.rb
BEGIN { puts "BEGIN code 1 block" } END { puts "END code 1 block" } END { puts "End code 2 block" } BEGIN { puts "Begin code 2 block" } # MAIN block code puts "MAIN code block"
执行结果:
zzl@zzl-VirtualBox:~/rubyprojects$ ruby demo_beginend2.rb
BEGIN code 1 block
Begin code 2 block
MAIN code block
End code 2 block
END code 1 block