婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > Ruby中的循環語句的用法教程

Ruby中的循環語句的用法教程

熱門標簽:高德地圖標注客服 白銀外呼paas系統 湖州u友防封電銷卡 地圖標注賺錢項目注冊 百度地圖標注自定義圖片 滴滴外呼系統 常德電銷平臺外呼系統軟件價格 電銷機器人廠商代理 徐州網絡外呼系統哪個好

 Ruby中的循環用于執行相同的代碼塊指定的次數。本章將詳細介紹Ruby支持的循環語句。
Ruby while 語句:
語法:

while conditional [do]
   code
end

執行代碼當條件為true時。while循環的條件是代碼中的保留字,換行,反斜杠(\)或一個分號隔開。
實例:

#!/usr/bin/ruby

$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

Ruby while 修辭符:
語法:

code while condition

OR

begin
  code
end while conditional

執行代碼,當條件為true。

如果while 修飾符緊跟一個begin 語句但是沒有 rescue 或 ensure 子句, 代碼被執行前一次條件求值。
實例:

#!/usr/bin/ruby

$i = 0
$num = 5
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
Inside the loop i = 3
Inside the loop i = 4

Ruby until 語句:

until conditional [do]
   code
end

執行代碼當條件為false。until 條件語句從代碼分離的保留字,換行符或分號。
語句:

#!/usr/bin/ruby

$i = 0
$num = 5

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
Inside the loop i = 4
Inside the loop i = 5

Ruby until 修辭符:
語法:

code until conditional

OR

begin
   code
end until conditional

執行代碼當條件為 false。

如果 until 修辭符跟著 begin 語句但沒有 rescue 或 ensure 子句, 代碼一旦被執行在條件求值之前。
例子:

#!/usr/bin/ruby

$i = 0
$num = 5
begin
  puts("Inside the loop i = #$i" )
  $i +=1;
end until $i > $num

這將產生以下結果:

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
Inside the loop i = 5

Ruby for 語句:
語法:

for variable [, variable ...] in expression [do]
   code
end

一次執行代碼的每個元素在 in 表達式。
實例:

#!/usr/bin/ruby

for i in 0..5
  puts "Value of local variable is #{i}"
end

這里我們定義的范圍 0 .. 5 。因為在語句 for i in 0..5 將允許取值的范圍從0到5(含5),這將產生以下結果:

Value of local variable is 0
Value of local variable is 1
Value of local variable is 2
Value of local variable is 3
Value of local variable is 4
Value of local variable is 5

 for...in 循環幾乎是完全等同于:

(expression).each do |variable[, variable...]| code end

除了一個for循環不創建一個新的局部變量的范圍。一個循環的表情從代碼分離,保留字,一個換行符,或分號。
例子:

#!/usr/bin/ruby

(0..5).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
Value of local variable is 4
Value of local variable is 5

Ruby break 語句:
語法:

break

終止大多數內部的循環。終止塊內的方法返回nil如果調用的方法與相關塊。
實例:

#!/usr/bin/ruby

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

Ruby next 語句:
語法:

next

跳轉到最內部循環的下一次迭代。如果調用塊一個塊內終止執行(帶 yield 或調用返回 nil )。
例子:

#!/usr/bin/ruby

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

Ruby redo 語句:
語法:

redo

會重新啟動啟動這個最內部的循環迭代,而不檢查循環條件。

會重新啟動 yield or call ,如果一個塊內調用。
例子:

#!/usr/bin/ruby

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
............................

Ruby retry 語句:
語法:

retry

如果 retry 表達出現在 rescue 子句,則從開始重新開始。

begin
  do_something # exception raised
rescue
  # handles error
  retry # restart from beginning
end

如果出現重試迭代,塊,或體內的表達,重新啟動迭代調用。迭代器的參數條件將重新計算。

for i in 1..5
  retry if some_condition # restart from i == 1
end

實例:

#!/usr/bin/ruby

for i in 1..5
  retry if i > 2
  puts "Value of local variable is #{i}"
end

這將產生以下結果,將進入無限循環:

Value of local variable is 1
Value of local variable is 2
Value of local variable is 1
Value of local variable is 2
Value of local variable is 1
Value of local variable is 2
............................

您可能感興趣的文章:
  • 使用Ruby來編寫訪問Twitter的命令行應用程序的教程
  • 幾個加速Ruby on Rails的編程技巧
  • 使用Ruby實現簡單的事物驅動的web應用的教程

標簽:永州 三沙 普洱 荊門 梧州 遼寧 公主嶺 張家界

巨人網絡通訊聲明:本文標題《Ruby中的循環語句的用法教程》,本文關鍵詞  Ruby,中的,循環,語句,的,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Ruby中的循環語句的用法教程》相關的同類信息!
  • 本頁收集關于Ruby中的循環語句的用法教程的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 海安县| 天峻县| 涞水县| 金秀| 获嘉县| 闽清县| 肥西县| 巢湖市| 肇州县| 双江| 浦北县| 响水县| 拜城县| 虹口区| 佛冈县| 荣昌县| 宣化县| 闸北区| 巴彦县| 章丘市| 桦甸市| 长泰县| 凌海市| 永顺县| 万荣县| 宁晋县| 四平市| 平江县| 瓦房店市| 泽州县| 定南县| 景德镇市| 都匀市| 衡东县| 城口县| 广安市| 红原县| 江津市| 安溪县| 武冈市| 黎城县|