ruby 流程控制 方法
更新時間:2007年11月24日 13:08:42 作者:
這章我們將討論更多的Ruby流程控制.
case
我們用case語句測試有次序的條件.正如我們所見的,這和C,Java的switch相當接近,但更強大.
ruby> i=8
ruby> case i
| when 1, 2..5
| print "1..5\n"
| when 6..10
| print "6..10\n"
| end
6..10
nil
2..5表示2到5之間的一個范圍.下面的表達式測試 i 是否在范圍內(nèi):
(2..5) === i
case 內(nèi)部也是用關系運算符 === 來同時測試幾個條件.為了保持ruby的面對對象性質(zhì), === 可以合適地理解為出現(xiàn)在 when 條件里的對
象.比如,下面的代碼現(xiàn)在第一個 when 里測試字符串是否相等,并在第二個 when 里進行正則表達式匹配.
ruby> case 'abcdef'
| when 'aaa', 'bbb'
| print "aaa or bbb\n"
| when /def/
| print "includes /def/\n"
| end
includes /def/
nil
while
雖然你將會在下一章發(fā)現(xiàn)并不需要經(jīng)常將循環(huán)體寫得很清楚,但 Ruby 還是提供了一套構建循環(huán)的好用的方法.
while 是重復的 if.我們在猜詞游戲和正則表達式中使用過它(見前面的章節(jié));這里,當條件(condition)為真的時候,它圍繞一個代碼域以
while condition...end的形式循環(huán).但 while 和 if 可以很容易就運用于單獨語句:
ruby> i = 0
0
ruby> print "It's zero.\n" if i==0
It's zero.
nil
ruby> print "It's negative.\n" if i<0
nil
ruby> print "#{i+=1}\n" while i<3
1
2
3
nil
有時候你想要否定一個測試條件. unless 是 if 的否定, until 是一個否定的 while.在這里我把它們留給你實驗.
There are four ways to interrupt the progress of a loop from inside. First, break means, as in C, to escape from the
loop entirely. Second, next skips to the beginning of the next iteration of the loop (corresponding to C's continue).
Third, ruby has redo, which restarts the current iteration. The following is C code illustrating the meanings of break,
next, and redo:
有四種從內(nèi)部中斷循環(huán)的方法.第一,和C一樣break從循環(huán)中完全退出.第二, next 跳到下一次循環(huán)迭代的開始(對應于C的 continue ).第
三,Ruby有redo,它可以重新開始現(xiàn)在的迭代.下面是用 C 代碼對break,next,redo的意義做了演示:
while (condition) {
label_redo:
goto label_next; /* ruby's "next" */
goto label_break; /* ruby's "break" */
goto label_redo; /* ruby's "redo" */
...
...
label_next:
}
label_break:
...
第四種方法是由循環(huán)內(nèi)跳出的方法是 returen. return的結果是不僅從循環(huán)中跳出,而且會從含循環(huán)的方法中跳出.如果有參數(shù),它會返回給方法調(diào)用,不然就返回nil.
for
C程序員現(xiàn)在會想知道怎樣做一個"for"循環(huán).Ruby的for比你想象的要有趣一點.下面的loop由集合中的元素控制運行:
for elt in collection
...
end
集合可以是一個數(shù)集(也是傳統(tǒng)意義上的for循環(huán)):
ruby> for num in (4..6)
| print num,"\n"
| end
4
5
6
4..6
也可以是其它的什么類型的集合,比如一個數(shù)組:
ruby> for elt in [100,-9.6,"pickle"]
| print "#{elt}\t(#{elt.type})\n"
| end
100 (Fixnum)
-9.6 (Float)
pickle (String)
[100, -9.6, "pickle"]
但我們說過頭了.for其實是 each 的另一寫法,正巧,這是我們關于迭代器的第一個例子.下面的兩種形式是等價的:
# If you're used to C or Java, you might prefer this.
for i in collection
...
end
# A Smalltalk programmer might prefer this.
collection.each {|i|
...
}
一旦你熟悉了迭代器,它便會常常代替?zhèn)鹘y(tǒng)的循環(huán).它們一般更容易處理.因此,讓我們接著學習更多關于迭代器的知識.
case
我們用case語句測試有次序的條件.正如我們所見的,這和C,Java的switch相當接近,但更強大.
ruby> i=8
ruby> case i
| when 1, 2..5
| print "1..5\n"
| when 6..10
| print "6..10\n"
| end
6..10
nil
2..5表示2到5之間的一個范圍.下面的表達式測試 i 是否在范圍內(nèi):
(2..5) === i
case 內(nèi)部也是用關系運算符 === 來同時測試幾個條件.為了保持ruby的面對對象性質(zhì), === 可以合適地理解為出現(xiàn)在 when 條件里的對
象.比如,下面的代碼現(xiàn)在第一個 when 里測試字符串是否相等,并在第二個 when 里進行正則表達式匹配.
ruby> case 'abcdef'
| when 'aaa', 'bbb'
| print "aaa or bbb\n"
| when /def/
| print "includes /def/\n"
| end
includes /def/
nil
while
雖然你將會在下一章發(fā)現(xiàn)并不需要經(jīng)常將循環(huán)體寫得很清楚,但 Ruby 還是提供了一套構建循環(huán)的好用的方法.
while 是重復的 if.我們在猜詞游戲和正則表達式中使用過它(見前面的章節(jié));這里,當條件(condition)為真的時候,它圍繞一個代碼域以
while condition...end的形式循環(huán).但 while 和 if 可以很容易就運用于單獨語句:
ruby> i = 0
0
ruby> print "It's zero.\n" if i==0
It's zero.
nil
ruby> print "It's negative.\n" if i<0
nil
ruby> print "#{i+=1}\n" while i<3
1
2
3
nil
有時候你想要否定一個測試條件. unless 是 if 的否定, until 是一個否定的 while.在這里我把它們留給你實驗.
There are four ways to interrupt the progress of a loop from inside. First, break means, as in C, to escape from the
loop entirely. Second, next skips to the beginning of the next iteration of the loop (corresponding to C's continue).
Third, ruby has redo, which restarts the current iteration. The following is C code illustrating the meanings of break,
next, and redo:
有四種從內(nèi)部中斷循環(huán)的方法.第一,和C一樣break從循環(huán)中完全退出.第二, next 跳到下一次循環(huán)迭代的開始(對應于C的 continue ).第
三,Ruby有redo,它可以重新開始現(xiàn)在的迭代.下面是用 C 代碼對break,next,redo的意義做了演示:
while (condition) {
label_redo:
goto label_next; /* ruby's "next" */
goto label_break; /* ruby's "break" */
goto label_redo; /* ruby's "redo" */
...
...
label_next:
}
label_break:
...
第四種方法是由循環(huán)內(nèi)跳出的方法是 returen. return的結果是不僅從循環(huán)中跳出,而且會從含循環(huán)的方法中跳出.如果有參數(shù),它會返回給方法調(diào)用,不然就返回nil.
for
C程序員現(xiàn)在會想知道怎樣做一個"for"循環(huán).Ruby的for比你想象的要有趣一點.下面的loop由集合中的元素控制運行:
for elt in collection
...
end
集合可以是一個數(shù)集(也是傳統(tǒng)意義上的for循環(huán)):
ruby> for num in (4..6)
| print num,"\n"
| end
4
5
6
4..6
也可以是其它的什么類型的集合,比如一個數(shù)組:
ruby> for elt in [100,-9.6,"pickle"]
| print "#{elt}\t(#{elt.type})\n"
| end
100 (Fixnum)
-9.6 (Float)
pickle (String)
[100, -9.6, "pickle"]
但我們說過頭了.for其實是 each 的另一寫法,正巧,這是我們關于迭代器的第一個例子.下面的兩種形式是等價的:
# If you're used to C or Java, you might prefer this.
for i in collection
...
end
# A Smalltalk programmer might prefer this.
collection.each {|i|
...
}
一旦你熟悉了迭代器,它便會常常代替?zhèn)鹘y(tǒng)的循環(huán).它們一般更容易處理.因此,讓我們接著學習更多關于迭代器的知識.
相關文章
win10下使用virtualbox + vagrant配置ruby開發(fā)機環(huán)境
Vagrant是一個基于Ruby的工具,用于創(chuàng)建和部署虛擬化開發(fā)環(huán)境。它 使用Oracle的開源VirtualBox虛擬化系統(tǒng),使用 Chef創(chuàng)建自動化虛擬環(huán)境。搭配Cmder如絲般順滑,實現(xiàn)你所有關于Mac&Linux的幻想2017-08-08Ruby on Rails實現(xiàn)最基本的用戶注冊和登錄功能的教程
這里我們主要以has_secure_password的用戶密碼驗證功能為中心,來講解Ruby on Rails實現(xiàn)最基本的用戶注冊和登錄功能的教程,需要的朋友可以參考下2016-06-06編寫Ruby腳本來對Twitter用戶的數(shù)據(jù)進行深度挖掘
這篇文章主要介紹了編寫Ruby腳本來對Twitter用戶的數(shù)據(jù)進行深度挖掘的一些例子,通過調(diào)用Twitter API來實現(xiàn)各種功能(內(nèi)地注意墻),需要的朋友可以參考下2015-11-11Ruby中使用連續(xù)體Continuation實現(xiàn)生成器
這篇文章主要介紹了Ruby中使用連續(xù)體Continuation實現(xiàn)生成器,本文先是介紹了生成器的概念,然后給出實現(xiàn)代碼,需要的朋友可以參考下2015-01-01