Ruby 简明教程 Part 3

1.简介

2.安装

3.基本语法

4高级进阶

 

 
------继续

3.基本语法

3.5 注释

3.5.1 单行注释用#开始;也能够将注释附在语句行尾。

# This is a single line comment. 
 
puts "Hello, Ruby!"
 
第一行是注释;只执行第二句。执行结果:
 
Hello, Ruby!

3.5.2 多行注释

使用 =begin 和 =end 句法, 格式以下:
 
 
 
 
=begin
This is a multiline comment and con spwan as many lines as you
like. But =begin and =end should come in the first line only. 
=end

3.6 if...else,case, unless 语句

3.6.1 if ... else 

格式以下:
 
if conditional [then]
   code...
[elsif conditional [then]
   code...]...
[else
   code...]
end
条件语句中false 和 nil 是false, 其它都为true。
 
demo_ifelsif.rb :
 
x = 1
if x > 2
   puts "x is greater than 2"
elsif x <= 2 and x!=0
   puts "x is 1"
else
   puts "I can't guess the number"
end
执行结果:
 
zzl@zzl-VirtualBox:~/rubyprojects$ ruby demo_ifelsif.rb
x is 1
 

3.6.2 if 变体

格式:
code if condition
 
若是条件成立,执行code
 

3.6.3 Unless语句

格式:
unless conditional [then]
   code1
[else
   code2 ]
end
条件不成立,执行code1;若是条件成立,执行 else 中的 code2.
 
 
x = 1 
unless x>=2
   puts "x is less than 2"
 else
   puts "x is greater than 2"
end
以上语句,执行结果:
 
x is less than 2

3.6.4 unless 变体

格式:
code unless conditional
 
条件不成立,执行code。
例子:
 
$var = 1
print "1 -- Value is set\n" if $var
print "2 -- Value is set\n" unless $var
 
$var = false
print "3 -- Value is set\n" unless $var
执行,获得以下结果:
 
1 -- Value is set
3 -- Value is set

3.6.5 case 语句

格式
case expression
[when expression [, expression ...] [then]
   code ]...
[else
   code ]
end
比较 case 的表达式 ,当when 的表达式匹配时, 执行对应的代码;若是都不匹配,则执行else 后的代码。
 
when 后的表达式用then, 新行,或分号;与后面的代码分开。例如:
 
case expr0
when expr1, expr2
   stmt1
when expr3, expr4
   stmt2
else
   stmt3
end
以上代码与下列代码等效:
 
_tmp = expr0
if expr1 === _tmp || expr2 === _tmp
   stmt1
elsif expr3 === _tmp || expr4 === _tmp
   stmt2
else
   stmt3
end
举例:demo_when.rb 
$age = 5
case $age
when 0 .. 2
   puts "baby"
when 3 .. 6
   puts "little child"
when 7 .. 12
   puts "child"
when 13 .. 18
   puts "youth"
else
   puts "adult"
end
执行结果:
 
zzl@zzl-VirtualBox:~/rubyprojects$ ruby demo_when.rb
little child
 
 

3.7 循环

3.7.1 while 语句

格式
while conditional [do]
   code
end
Executes code while conditional is true. A while loop's conditional is separated from code by the reserved word do, a newline, backslash \, or a semicolon ;.条件成立,执行代码code。 条件用do, 新行, \ , 或分号;与代码分开。
 
 
$i = 0
$num = 5
 
while $i < $num do
   puts("Inside the loop i = #$i" )
   $i +=1
end
运行结果:
 
Inside the loop i = 0
Inside the loop i = 1
Inside the loop i = 2
Inside the loop i = 3
Inside the loop i = 4

3.7.2 while 变体 

格式
code while condition
 
 
begin 
  code 
end while conditional
先执行代码,再查验条件。代码至少执行一次。只要条件知足,就继续执行代码。
 
 
 
 
$i = 0
$num = 3
begin
   puts("Inside the loop i = #$i" )
   $i +=1
end while $i < $num
执行结果:
 
Inside the loop i = 0
Inside the loop i = 1
Inside the loop i = 2
 

3.7.3 until 语句

until conditional [do]
   code
end
条件不成立是,执行code代码;用do,新行,分号分隔条件和代码。
 
 
$i = 0
$num = 3
 
until $i > $num do
   puts("Inside the loop i = #$i" )
   $i +=1;
end
运行结果:
 
Inside the loop i = 0
Inside the loop i = 1
Inside the loop i = 2
Inside the loop i = 3
 

3.7.4 until 变体

格式
code until conditional
 
 
begin
   code
end until conditional
 执行代码code, 当条件不成立
 
当 until 跟在begin 以后,则在条件执行前,至少执行一次。
 

3.7.5 for 语句

格式
for variable [, variable ...] in expression [do]
   code
end
表达式中的每一个元素,都执行一次代码code。
 
 
for i in 0..3
   puts "Value of local variable is #{i}"
end
结果:
 
Value of local variable is 0
Value of local variable is 1
Value of local variable is 2
Value of local variable is 3
 
 for...in expression 循环,几乎等同于如下循环
 
(expression).each do |variable[, variable...]| code end
不过for循环不建立新的局部变量scope。
 
 
(0..3).each do |i|
   puts "Value of local variable is #{i}"
end
结果:
 
Value of local variable is 0
Value of local variable is 1
Value of local variable is 2
Value of local variable is 3
 

3.7.6 break 语句

格式
break
 结束最内部循环。方法返回nil.
 
 
for i in 0..5
   if i > 2 then
      break
   end
   puts "Value of local variable is #{i}"
end
结果为:
 
Value of local variable is 0
Value of local variable is 1
Value of local variable is 2

3.7.7 next 语句

格式
next
 跳转到最内循环的下一次循环。结束执行块若是在块内调用。
 
 
 
for i in 0..5
   if i < 2 then
      next
   end
   puts "Value of local variable is #{i}"
end
结果为:
 
Value of local variable is 2
Value of local variable is 3
Value of local variable is 4
Value of local variable is 5

3.7.8 redo 语句

格式
redo
 从新执行最内循环,而不检查循环条件。若是在块内调用,怎从新启动 yield 或调用。
 
 
for i in 0..5
   if i < 2 then
      puts "Value of local variable is #{i}"
      redo
   end
end
这将致使死循环:
 
Value of local variable is 0
Value of local variable is 0
............................

3.7.9 retry 语句

格式
retry
 
retry 出如今begin 表达式的rescue 子句,则从 begin 主体从新执行。
 
begin
   do_something # exception raised
rescue
   # handles error
   retry # restart from beginning
end
 
 
 

举例:

begin
  puts "Iteration"
  raise
rescue
  retry
end

结果将致使死循环:html


Iteration
Iteration
Iteration
Iteration
Iteration
Iteration
Iteration
Iteration
Iteration