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

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

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

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

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

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

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

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

while conditional [do]
   code
end

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

?
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 修辭符:
語(yǔ)法:

code while condition

OR

begin
  code
end while conditional

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

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

?
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 語(yǔ)句:

until conditional [do]
   code
end

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

?
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 修辭符:
語(yǔ)法:

code until conditional

OR

begin
   code
end until conditional

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

如果 until 修辭符跟著 begin 語(yǔ)句但沒(méi)有 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 語(yǔ)句:
語(yǔ)法:

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

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

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

這里我們定義的范圍 0 .. 5 。因?yàn)樵谡Z(yǔ)句 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

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

?
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 語(yǔ)句:
語(yǔ)法:

break

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

?
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 語(yǔ)句:
語(yǔ)法:

next

跳轉(zhuǎn)到最內(nèi)部循環(huán)的下一次迭代。如果調(diào)用塊一個(gè)塊內(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 語(yǔ)句:
語(yǔ)法:

redo

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

會(huì)重新啟動(dòng) yield or call ,如果一個(gè)塊內(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í)行無(wú)限循環(huán):

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

Ruby retry 語(yǔ)句:
語(yǔ)法:

retry

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

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

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

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

實(shí)例:

?
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é)果,將進(jìn)入無(wú)限循環(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簡(jiǎn)要說(shuō)明Ruby中的迭代器

    簡(jiǎn)要說(shuō)明Ruby中的迭代器

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

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

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

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

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

    Ruby迭代器的7種技巧分享

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

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

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

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

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

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

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

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

    Ruby簡(jiǎn)潔學(xué)習(xí)筆記(一):字符串、數(shù)字、類(lèi)和對(duì)象

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

    腳本之家2472020-04-20
  • RubyRuby進(jìn)行文件信息輸出實(shí)例代碼

    Ruby進(jìn)行文件信息輸出實(shí)例代碼

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

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

    剖析 Ruby 訪問(wèn)控制

    前面,我們說(shuō) Ruby 沒(méi)有函數(shù),只有方法.而且實(shí)際上有不止一種方法.這一節(jié)我們介紹 訪問(wèn)控制 (accesscontrols). 想想當(dāng)我們?cè)谧罡邔佣皇窃谝粋€(gè)類(lèi)的定義里定義...

    ruby教程網(wǎng)3572020-04-08
主站蜘蛛池模板: 无人区在线观看免费国语完整版 | 吃瓜视频在线观看 | 4虎影视国产在线观看精品 4s4s4s4s色大众影视 | 免费高清视频免费观看 | 日不卡| 国内免费高清视频在线观看 | 99re8在这里只有精品2 | 嫩草在线视频www免费观看 | 草草线在成年免费视频网站 | 欧美日韩亚洲另类人人澡 | 日本免费不卡在线一区二区三区 | 韩国久播影院理论片不卡影院 | 天天干天天色综合 | av在线色| 亚洲精品久久久久AV无码 | 午夜福利试看120秒体验区 | 东北老妇露脸xxxxx | 艹的好爽| 精品久久久久久久久久久久久久久 | 极品妖艳许清赵丽全文免费阅读 | 国产精品成人一区二区 | 99九九国产精品免费视频 | 丫鬟粗大狠狠贯穿h | 久久精品热只有精品 | 95在线观看精品视频 | 日本欧美不卡一区二区三区在线 | 亚洲精品91 | 91香蕉视频在线播放 | 国产欧美曰韩一区二区三区 | 我和么公的秘密小说免费 | 欧美肥乳 | 欧美日韩看看2015永久免费 | 午夜爱爱爱爱爽爽爽视频网站 | 青青青国产手机在线播放 | 欧美乱妇高清无乱码视频在线 | 亚洲性网 | 四虎传媒 | 午夜精品久久久内射近拍高清 | 国产卡一卡二卡3卡乱码免费 | 性啪啪chinese东北女人 | 扒开斗罗美女了的胸罩和内裤漫画 |