一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術(shù)及教程分享平臺!
分類導(dǎo)航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Ruby - Ruby中的循環(huán)語句的用法教程

Ruby中的循環(huán)語句的用法教程

2020-04-28 10:02腳本之家 Ruby

這篇文章主要介紹了Ruby中的循環(huán)語句的用法教程,邏輯循環(huán)語句是每門編程語言的基礎(chǔ),需要的朋友可以參考下

 Ruby中的循環(huán)用于執(zhí)行相同的代碼塊指定的次數(shù)。本章將詳細介紹Ruby支持的循環(huán)語句
Ruby while 語句:
語法:

while conditional [do]
   code
end

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

?
1
2
3
4
5
6
7
8
9
#!/usr/bin/ruby
 
$i = 0
$num = 5
 
while $i < $num do
  puts("Inside the loop i = #$i" )
  $i +=1
end

這將產(chǎn)生以下結(jié)果:

?
1
2
3
4
5
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

執(zhí)行代碼,當(dāng)條件為true。

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

?
1
2
3
4
5
6
7
8
#!/usr/bin/ruby
 
$i = 0
$num = 5
begin
  puts("Inside the loop i = #$i" )
  $i +=1
end while $i < $num

這將產(chǎn)生以下結(jié)果:

?
1
2
3
4
5
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

執(zhí)行代碼當(dāng)條件為false。until 條件語句從代碼分離的保留字,換行符或分號。
語句:

?
1
2
3
4
5
6
7
8
9
#!/usr/bin/ruby
 
$i = 0
$num = 5
 
until $i > $num do
  puts("Inside the loop i = #$i" )
  $i +=1;
end

這將產(chǎn)生以下結(jié)果:

?
1
2
3
4
5
6
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

執(zhí)行代碼當(dāng)條件為 false。

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

?
1
2
3
4
5
6
7
8
#!/usr/bin/ruby
 
$i = 0
$num = 5
begin
  puts("Inside the loop i = #$i" )
  $i +=1;
end until $i > $num

這將產(chǎn)生以下結(jié)果:

?
1
2
3
4
5
6
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

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

?
1
2
3
4
5
#!/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),這將產(chǎn)生以下結(jié)果:

?
1
2
3
4
5
6
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 循環(huán)幾乎是完全等同于:

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

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

?
1
2
3
4
5
#!/usr/bin/ruby
 
(0..5).each do |i|
  puts "Value of local variable is #{i}"
end

這將產(chǎn)生以下結(jié)果:

?
1
2
3
4
5
6
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

終止大多數(shù)內(nèi)部的循環(huán)。終止塊內(nèi)的方法返回nil如果調(diào)用的方法與相關(guān)塊。
實例:

?
1
2
3
4
5
6
7
8
#!/usr/bin/ruby
 
for i in 0..5
  if i > 2 then
   break
  end
  puts "Value of local variable is #{i}"
end

這將產(chǎn)生以下結(jié)果:

?
1
2
3
Value of local variable is 0
Value of local variable is 1
Value of local variable is 2

Ruby next 語句:
語法:

next

跳轉(zhuǎn)到最內(nèi)部循環(huán)的下一次迭代。如果調(diào)用塊一個塊內(nèi)終止執(zhí)行(帶 yield 或調(diào)用返回 nil )。
例子:

?
1
2
3
4
5
6
7
8
#!/usr/bin/ruby
 
for i in 0..5
  if i < 2 then
   next
  end
  puts "Value of local variable is #{i}"
end

這將產(chǎn)生以下結(jié)果:

?
1
2
3
4
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

會重新啟動啟動這個最內(nèi)部的循環(huán)迭代,而不檢查循環(huán)條件。

會重新啟動 yield or call ,如果一個塊內(nèi)調(diào)用。
例子:

?
1
2
3
4
5
6
7
8
#!/usr/bin/ruby
 
for i in 0..5
  if i < 2 then
   puts "Value of local variable is #{i}"
   redo
  end
end

這將產(chǎn)生以下結(jié)果,將執(zhí)行無限循環(huán):

?
1
2
3
Value of local variable is 0
Value of local variable is 0
............................

Ruby retry 語句:
語法:

retry

如果 retry 表達出現(xiàn)在 rescue 子句,則從開始重新開始。

?
1
2
3
4
5
6
begin
  do_something # exception raised
rescue
  # handles error
  retry # restart from beginning
end

如果出現(xiàn)重試迭代,塊,或體內(nèi)的表達,重新啟動迭代調(diào)用。迭代器的參數(shù)條件將重新計算。

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

實例:

?
1
2
3
4
5
6
#!/usr/bin/ruby
 
for i in 1..5
  retry if i > 2
  puts "Value of local variable is #{i}"
end

這將產(chǎn)生以下結(jié)果,將進入無限循環(huán):

?
1
2
3
4
5
6
7
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簡要說明Ruby中的迭代器

    簡要說明Ruby中的迭代器

    這篇文章主要介紹了Ruby中的迭代器,迭代器的概念在動態(tài)語言的編程中十分重要,文章中介紹了Ruby中的each迭代器和collect迭代器,需要的朋友可以參考下 ...

    goldensun2772020-04-25
  • RubyCentOS中配置Ruby on Rails環(huán)境

    CentOS中配置Ruby on Rails環(huán)境

    經(jīng)過一個上午的折騰,終于把ROR環(huán)境在CentOS中搞定,繞了很多彎路,把文章寫下來總結(jié)一下 ...

    可樂加糖4762020-04-12
  • RubyRuby迭代器的7種技巧分享

    Ruby迭代器的7種技巧分享

    這篇文章主要介紹了Ruby迭代器的7種技巧分享,Ruby中的迭代器非常人性化,本文既是講解了7個技巧也是講解了7種迭代器,需要的朋友可以參考下 ...

    腳本之家4782020-04-20
  • RubyRuby環(huán)境下安裝使用bundler來管理多版本的gem

    Ruby環(huán)境下安裝使用bundler來管理多版本的gem

    這篇文章主要介紹了Ruby環(huán)境下安裝使用bundler來管理多版本的gem的方法,舉了Ruby On Rails中的應(yīng)用實例來進行演示,需要的朋友可以參考下 ...

    日拱一卒4332020-05-10
  • RubyRuby設(shè)計模式編程中使用Builder建造者模式的實例

    Ruby設(shè)計模式編程中使用Builder建造者模式的實例

    這篇文章主要介紹了Ruby設(shè)計模式編程中使用Builder建造者模式的實例,建造者模式將一個復(fù)雜對象的構(gòu)造與它的表示分離,使同樣的構(gòu)建過程可以創(chuàng)建不同的表...

    范孝鵬2192020-05-07
  • RubyRuby簡潔學(xué)習(xí)筆記(一):字符串、數(shù)字、類和對象

    Ruby簡潔學(xué)習(xí)筆記(一):字符串、數(shù)字、類和對象

    這篇文章主要介紹了Ruby簡潔學(xué)習(xí)筆記(一):字符串、數(shù)字、類和對象,本文是學(xué)習(xí)筆記第一篇,需要的朋友可以參考下 ...

    腳本之家2472020-04-20
  • RubyRuby進行文件信息輸出實例代碼

    Ruby進行文件信息輸出實例代碼

    Ruby進行文件信息輸出實例代碼,數(shù)據(jù)是隨機的,所以每次的記錄都會不同。 ...

    ruby教程網(wǎng)2962020-04-10
  • Ruby剖析 Ruby 訪問控制

    剖析 Ruby 訪問控制

    前面,我們說 Ruby 沒有函數(shù),只有方法.而且實際上有不止一種方法.這一節(jié)我們介紹 訪問控制 (accesscontrols). 想想當(dāng)我們在最高層而不是在一個類的定義里定義...

    ruby教程網(wǎng)3572020-04-08
主站蜘蛛池模板: 免费网站直接进入 | 精品无码国产AV一区二区三区 | 色老大在线 | 狠狠狠地啪香蕉 | 日本高清全集免费观看 | 国产一卡二卡3卡4卡四卡在线 | 特黄特色大片免费视频播放 | 欧美日韩在线一区二区三区 | 色综合图区 | 亚洲欧美久久久久久久久久爽网站 | ipx 在线播放| 亚洲国产婷婷俺也色综合 | 天天操天天射天天爽 | 成年极品漫画在线观看 | 狠狠色狠狠色综合日日小蛇 | jj视频免费 | 欧美日韩一区二区三区在线观看 | 99视频免费在线观看 | 图片专区亚洲欧美另类 | 成人综合网址 | 亚洲 制服 欧美 中文字幕 | 四虎官网 | 视频一区二区国产 | 国产高清日韩 | aaa黄色| 经典千人斩一区二区视频 | 特级淫片大乳女子高清视频 | 免费在线观看亚洲 | 国产精品久久现线拍久青草 | 好看华人华人经典play | 精品99一区二区三区麻豆 | 成人亚洲欧美综合 | 国产91短视频 | 手机看片一区二区 | 欧美亚洲国产另类 | 亚洲精品国产乱码AV在线观看 | 精品女同同性视频很黄很色 | 亚洲国产日韩欧美一区二区三区 | pregnant欧美孕交xxx | 丝瓜香蕉视频 | 免费黄色网站视频 |