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

結果將導致死循環:

Iteration
Iteration
Iteration
Iteration
Iteration
Iteration
Iteration
Iteration
Iteration

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章