自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

認(rèn)識(shí)6個(gè)被誤解的Ruby特性

開發(fā) 后端
假設(shè)您是一名 C++ 開發(fā)人員,您需要使用 Ruby 快速執(zhí)行一些原型設(shè)計(jì)。當(dāng)您拿起一本 Ruby 參考書籍(比如 Pickaxe)或?yàn)g覽 Ruby 網(wǎng)站時(shí),會(huì)看到一些熟悉的構(gòu)造,比如類聲明、線程支持和異常處理。正當(dāng)您認(rèn)為自己了解 Ruby 的工作原理之時(shí),您意識(shí)到了,您 Ruby 代碼中的并發(fā)機(jī)制與 Boost 線程工作原理不一樣,catch 和 throw 也與它們看上去的大不相同,而且其他人在其 Ruby 腳本中各處使用了名為 self 的關(guān)鍵詞。歡迎來(lái)到 Ruby 的世界中!

簡(jiǎn)介: 假設(shè)您是一名 C++ 開發(fā)人員,您需要使用 Ruby 快速執(zhí)行一些原型設(shè)計(jì)。當(dāng)您拿起一本 Ruby 參考書籍(比如 Pickaxe)或?yàn)g覽 Ruby 網(wǎng)站時(shí),會(huì)看到一些熟悉的構(gòu)造,比如類聲明、線程支持和異常處理。正當(dāng)您認(rèn)為自己了解 Ruby 的工作原理之時(shí),您意識(shí)到了,您 Ruby 代碼中的并發(fā)機(jī)制與 Boost 線程工作原理不一樣,catch 和 throw 也與它們看上去的大不相同,而且其他人在其 Ruby 腳本中各處使用了名為 self 的關(guān)鍵詞。歡迎來(lái)到 Ruby 的世界中!

如果您是一名 C++ 程序員且需要在 Ruby 環(huán)境中工作,那么您有一些功課要做。本文討論了 Ruby 新手可能會(huì)誤解的六個(gè) Ruby 特性,特別是當(dāng)他或她來(lái)自一個(gè)類似但又不太相同的環(huán)境,比如 C++:

● Ruby 類層次結(jié)構(gòu)

● Ruby 中的單例方法

● self 關(guān)鍵詞

● method_missing 方法

● 異常處理

● 線程

注意:本文中所有的代碼均進(jìn)行測(cè)試,且基于 Ruby 版本 1.8.7。

Ruby 中的類層次結(jié)構(gòu)

Ruby 中的類層次結(jié)構(gòu)會(huì)很棘手。創(chuàng)建一個(gè) Cat 類型的類并開始探討其層次結(jié)構(gòu)(參見 清單 1)。

清單 1. Ruby 中的隱式類層次結(jié)構(gòu)

  1. irb(main):092:0class Cat  
  2. irb(main):093:1> end  
  3. => nil  
  4.    
  5. irb(main):087:0> c = Cat.new 
  6. => #<Cat:0x2bacb68>  
  7. irb(main):088:0> c.class 
  8. => Cat  
  9. irb(main):089:0> c.class.superclass  
  10. => Object  
  11. irb(main):090:0> c.class.superclass.superclass  
  12. => nil  
  13. irb(main):091:0> c.class.superclass.superclass.superclass  
  14. NoMethodError: undefined method `superclass' for nil:NilClass  
  15.         from (irb):91 
  16.         from :0 

Ruby 中的所有對(duì)象(甚至用戶定義的對(duì)象)都是 Object 類的后代,這在清單 1 中清晰可見。這與 C++ 是鮮明的對(duì)比。這一點(diǎn)也不像普通數(shù)據(jù)類型,例如 C/C++ int 或 double。清單 2 顯示了整數(shù) 1 的類層次結(jié)構(gòu)。

清單 2. 整數(shù) 1 的類層次結(jié)構(gòu)

  1. irb(main):100:01.class 
  2. => Fixnum  
  3. irb(main):101:01.class.superclass  
  4. => Integer  
  5. irb(main):102:01.class.superclass.superclass  
  6. => Numeric  
  7. irb(main):103:01.class.superclass.superclass.superclass  
  8. => Object 

到目前為止一切順利?,F(xiàn)在您知道了類本身是 Class 類型的對(duì)象。而 Class 最終派生自 Object,如 清單 3 中所示使用 Ruby 內(nèi)置的 String 類。

清單 3. 類的類層次結(jié)構(gòu)

  1. irb(main):100:0> String.class 
  2. => Class  
  3. irb(main):101:0> String.class.superclass  
  4. => Module  
  5. irb(main):102:0> String.class.superclass.superclass  
  6. => Object 

Module 是 Class 的基類,但是使用它時(shí)有一點(diǎn)要注意,即您不能直接實(shí)例化用戶定義的 Module 對(duì)象。如果您不想深入 Ruby 內(nèi)部,最好考慮與 C++ 命名空間有類似特征的 Module:您可以定義您自己的方法、常量、等等。您在 Class 中包含了一個(gè) Module,以及 voilà,Module 的所有元素現(xiàn)在會(huì)魔法般地成為 Class 的元素。清單 4 提供了一個(gè)示例。

清單 4. Module 不能進(jìn)行直接實(shí)例化,并且只能與類一同使用

  1. irb(main):020:0> module MyModule  
  2. irb(main):021:1> def hello  
  3. irb(main):022:2> puts "Hello World" 
  4. irb(main):023:2> end  
  5. irb(main):024:1> end  
  6. irb(main):025:0> test = MyModule.new 
  7. NoMethodError: undefined method `newfor MyModule:Module  
  8.         from (irb):25 
  9. irb(main):026:0class MyClass  
  10. irb(main):027:1> include MyModule  
  11. irb(main):028:1> end  
  12. => MyClass  
  13. irb(main):029:0> test = MyClass.new 
  14. => #<MyClass:0x2c18bc8>  
  15. irb(main):030:0> test.hello  
  16. Hello World  
  17. => nil 

下面再重申一下重點(diǎn):當(dāng)您使用 Ruby 編寫 c = Cat.new 時(shí),c 是派生自 Object 的 Cat 類型的一個(gè)對(duì)象。Cat 類是 Class 類型的一個(gè)對(duì)象,Class 派生自 Module,而 Module 又派生自 Object。因此該對(duì)象及其類型都是有效的 Ruby 對(duì)象。

#p#

單例方法和可編輯類

現(xiàn)在,看一下單例方法。假設(shè)您想使用 C++ 建模類似于人類社會(huì)的東西。那么您會(huì)如何做呢?定義一個(gè)名為 Human 的類,然后定義數(shù)百萬(wàn)的 Human 對(duì)象?這更像是在建模一個(gè)呆板的社會(huì);每個(gè)人必須具惟一的特征。Ruby 的單例方法在這里就派上了用場(chǎng),如 清單 5 所示。

清單 5. Ruby 中的單例方法

  1. irb(main):113:0> y = Human.new 
  2. => #<Human:0x319b6f0>  
  3. irb(main):114:0> def y.paint  
  4. irb(main):115:1> puts "Can paint" 
  5. irb(main):116:1> end  
  6. => nil  
  7. irb(main):117:0> y.paint  
  8. Can paint  
  9. => nil  
  10. irb(main):118:0> z = Human.new 
  11. => #<Human:0x3153fc0>  
  12. irb(main):119:0> z.paint  
  13. NoMethodError: undefined method `paint' for #<Human:0x3153fc0>  
  14.         from (irb):119 

Ruby 中的單例方法 是僅與特定對(duì)象關(guān)聯(lián)的方法,不能用于一般的類。它們的前綴是對(duì)象名稱。在 清單 5 中,paint 方法特定于 y對(duì)象,而且僅限于 y 對(duì)象;z.paint 導(dǎo)致一個(gè) “方法未定義” 錯(cuò)誤。您可以調(diào)用 singleton_methods 來(lái)查明一個(gè)對(duì)象中的單例方法列表:

  1. irb(main):120:0> y.singleton_methods  
  2. => ["paint"

不過(guò)在 Ruby 中有另一種定義單例方法的方式。看看 清單 6 中的代碼。

清單 6. 創(chuàng)建單例方法的另一種方式

  1. irb(main):113:0> y = Human.new 
  2. => #<Human:0x319b6f0>  
  3. irb(main):114:0class << y  
  4. irb(main):115:1> def sing  
  5. irb(main):116:1> puts "Can sing" 
  6. irb(main):117:1> end  
  7. irb(main):118:1>end  
  8. => nil  
  9. irb(main):117:0> y.sing  
  10. Can sing  
  11. => nil 

清單 5 還開創(chuàng)了新的可能性,可以添加新方法到用戶定義的類和內(nèi)置的 Ruby 現(xiàn)有類,比如 String。這在 C++ 中是不可能實(shí)現(xiàn)的,除非您能夠訪問您使用的類的源代碼。再次觀察 String 類(清單 7)。

清單 7. Ruby 允許您修改一個(gè)現(xiàn)有的類

  1. irb(main):035:0> y = String.new("racecar")  
  2. => "racecar" 
  3. irb(main):036:0> y.methods.grep(/palindrome/)  
  4. => [ ]  
  5. irb(main):037:0class String  
  6. irb(main):038:1> def palindrome?  
  7. irb(main):039:2> self == self.reverse  
  8. irb(main):040:2> end  
  9. irb(main):041:1> end  
  10. irb(main):050:0> y.palindrome?  
  11. => true 

清單 7 清楚地展示了如何編輯一個(gè)現(xiàn)有的 Ruby 類來(lái)添加您自行選擇的方法。這里,我添加了 palindrome? 方法到 String 類。因此 Ruby 類在運(yùn)行時(shí)是可編輯的(一個(gè)強(qiáng)大的屬性)。

現(xiàn)在您對(duì) Ruby 的類層次結(jié)構(gòu)和單例有了一定的認(rèn)識(shí),接下來(lái)我們來(lái)看 self。注意,在定義 palindrome? 方法時(shí)我使用了 self。

#p#

發(fā)現(xiàn) self

self 關(guān)鍵詞的最常見用法可能就是在 Ruby 類中聲明一個(gè)靜態(tài)方法,如 清單 8 所示。

清單 8. 使用 self 聲明類的靜態(tài)方法

  1. class SelfTest  
  2.    def self.test  
  3.       puts "Hello World with self!" 
  4.    end  
  5. end  
  6.    
  7. class SelfTest2  
  8.    def test  
  9.       puts "This is not a class static method" 
  10.    end  
  11. end  
  12.    
  13. SelfTest.test  
  14. SelfTest2.test 

從 清單 8 的輸出中可以看到(如 清單 9 所示),沒有對(duì)象您無(wú)法調(diào)用非靜態(tài)方法。該行為類似于 C++。

清單 9. 在沒有對(duì)象的情況下調(diào)用非靜態(tài)方法時(shí)會(huì)出錯(cuò)

  1. irb(main):087:0> SelfTest.test  
  2. Hello World with self!  
  3. => nil  
  4. irb(main):088:0> SelfTest2.test  
  5. NoMethodError: undefined method 'test' for SelfTest2:Class  
  6.         from (irb):88 

在探討 self 更深?yuàn)W的用途和含義之前,注意您也可以通過(guò)在方法名稱前面加上類名來(lái)在 Ruby 中定義一個(gè)靜態(tài)方法:

  1. class TestMe  
  2.    def TestMe.test  
  3.        puts "Yet another static member function" 
  4.    end  
  5. end  
  6.    
  7. TestMe.test  # works fine 

清單 10 提供了 self 的一個(gè)更有趣但不太容易找到的用法。

清單 10. 使用元類來(lái)聲明靜態(tài)方法

  1. class MyTest  
  2.    class << self  
  3.      def test  
  4.         puts "This is a class static method" 
  5.      end  
  6.    end  
  7. end  
  8.    
  9. MyTest.test   # works fine 

該段代碼以一種稍微不同的方式將 test 定義為一個(gè)類靜態(tài)方法。要了解究竟發(fā)生了什么,您需要看一下 class << self 語(yǔ)法的一些細(xì)節(jié)。class << self … end 創(chuàng)建一個(gè)元類。在方法查找鏈中,在訪問對(duì)象的基類之前先搜索該對(duì)象的元類。如果您在元類中定義一個(gè)方法,可以在類上調(diào)用該方法。這類似于 C++ 中靜態(tài)方法的概念。

可以訪問一個(gè)元類嗎?是的:只需從 class << self … end 內(nèi)返回 self。注意,在一個(gè) Ruby 類聲明中,您沒有義務(wù)僅給出方法定義。清單 11 顯示了元類。

清單 11. 理解元類

  1. irb(main):198:0class MyTest  
  2. irb(main):199:1> end  
  3. => nil  
  4. irb(main):200:0> y = MyTest.new 
  5. => #< MyTest:0x2d43fe0>  
  6. irb(main):201:0> z = class MyTest  
  7. irb(main):202:1class << self  
  8. irb(main):203:2> self  
  9. irb(main):204:2> end  
  10. irb(main):205:1> end  
  11. => #<Class: MyTest >  
  12. irb(main):206:0> z.class 
  13. => Class  
  14. irb(main):207:0> y.class 
  15. => MyTest 

回到 清單 7 的代碼,您會(huì)看到 palindrome 被定義為 self == self.reverse。在該上下文中,self 與 C++ 沒有什么區(qū)別。C++和 Ruby 中的方法都需要一個(gè)操作對(duì)象,以修改或提取狀態(tài)信息。self 是指這里的這個(gè)對(duì)象。注意,可以通過(guò)附加 self 前綴來(lái)選擇性地調(diào)用公共方法,指明方法付諸作用的對(duì)象,如 清單 12 所示。

清單 12. 使用 self 調(diào)用方法

  1. irb(main):094:0class SelfTest3  
  2. irb(main):095:1> def foo  
  3. irb(main):096:2> self.bar()  
  4. irb(main):097:2> end  
  5. irb(main):098:1> def bar  
  6. irb(main):099:2> puts "Testing Self" 
  7. irb(main):100:2> end  
  8. irb(main):101:1> end  
  9. => nil  
  10. irb(main):102:0> test = SelfTest3.new 
  11. => #<SelfTest3:0x2d15750>  
  12. irb(main):103:0> test.foo  
  13. Testing Self  
  14. => nil 

在 Ruby 中您無(wú)法通過(guò)附加 self 關(guān)鍵詞前綴來(lái)調(diào)用私有方法。對(duì)于一名 C++ 開發(fā)人員,這可能會(huì)有點(diǎn)混淆。清單 13 中的代碼明確表示,self 不能用于私有方法:對(duì)私有方法的調(diào)用只能針對(duì)隱式對(duì)象。

清單 13. self 不能用于私有方法調(diào)用

  1. irb(main):110:0class SelfTest4  
  2. irb(main):111:1> def method1  
  3. irb(main):112:2> self.method2  
  4. irb(main):113:2> end  
  5. irb(main):114:1> def method3  
  6. irb(main):115:2> method2  
  7. irb(main):116:2> end  
  8. irb(main):117:1private 
  9. irb(main):118:1> def method2  
  10. irb(main):119:2> puts "Inside private method" 
  11. irb(main):120:2> end  
  12. irb(main):121:1> end  
  13. => nil  
  14. irb(main):122:0> y = SelfTest4.new 
  15. => #<SelfTest4:0x2c13d80>  
  16. irb(main):123:0> y.method1  
  17. NoMethodError: private method `method2' called for #<SelfTest4:0x2c13d80>  
  18.         from (irb):112:in `method1'  
  19. irb(main):124:0> y.method3  
  20. Inside private method  
  21. => nil 

由于 Ruby 中的一切都是對(duì)象,當(dāng)在 irb 提示符上調(diào)用 self 時(shí)您會(huì)得到以下結(jié)果:

  1. irb(main):104:0> self  
  2. => main  
  3. irb(main):105:0> self.class 
  4. => Object 

一啟動(dòng) irb,Ruby 解釋器就為您創(chuàng)建主對(duì)象。這一主對(duì)象在 Ruby 相關(guān)的文章中也被稱為頂層上下文。

關(guān)于 self 就介紹這么多了。下面我們接著來(lái)看動(dòng)態(tài)方法和 method_missing 方法。

#p#

method_missing 揭秘

看一下 清單 14 中的 Ruby 代碼。

清單 14. 運(yùn)行中的 method_missing

  1. irb(main):135:0class Test  
  2. irb(main):136:1> def method_missing(method, *args)  
  3. irb(main):137:2> puts "Method: #{method} Args: (#{args.join(', ')})" 
  4. irb(main):138:2> end  
  5. irb(main):139:1> end  
  6. => nil  
  7. irb(main):140:0> t = Test.new 
  8. => #<Test:0x2c7b850>  
  9. irb(main):141:0> t.f(23)  
  10. Method: f Args: (23)  
  11. => nil 

顯然,如果 voodoo 是您喜歡的,那么清單 14 會(huì)給您這個(gè)恩典。這里發(fā)生什么了呢?我們創(chuàng)建了一個(gè) Test 類型的對(duì)象,然后調(diào)用了t.f,以 23 作為參數(shù)。但是 Test 沒有以 f 作為方法,您應(yīng)當(dāng)會(huì)得到一個(gè) NoMethodError 或類似的錯(cuò)誤消息。Ruby 在這里做了一件很棒的事情:您的方法調(diào)用被阻截并由 method_missing 處理。method_missing 的第一個(gè)參數(shù)是缺失的方法名,在本例中是f。第二個(gè)(也是最后一個(gè))參數(shù)是 *args,該參數(shù)捕獲傳遞給 f 的參數(shù)。您可以在何處使用像這樣的參數(shù)呢?在眾多選項(xiàng)之中,您可以輕松地將方法調(diào)用轉(zhuǎn)發(fā)到一個(gè)包含的 Module 或一個(gè)組件對(duì)象,而不為頂級(jí)類中的每個(gè)調(diào)用顯式提供一個(gè)包裝應(yīng)用程序編程接口。

在 清單 15 中查看更多 voodoo。

清單 15. 使用 send 方法將參數(shù)傳遞給一個(gè)例程

  1. irb(main):142:0class Test  
  2. irb(main):143:1> def method1(s, y)  
  3. irb(main):144:2> puts "S: #{s} Y: #{y}" 
  4. irb(main):145:2> end  
  5. irb(main):146:1> end  
  6. => nil  
  7. irb(main):147:0>t = Test.new 
  8. irb(main):148:0> t.send(:method1, 2312)  
  9. S: 23 Y: 12 
  10. => nil 

在 清單 15 中,class Test 有一個(gè)名為 method1 的方法被定義。但是,這里沒有直接調(diào)用方法,而是發(fā)出對(duì) send 方法的調(diào)用。send 是 Object 類的一個(gè)公共方法,因此可用于 Test(記住,所有類都派生自 Object)。send 方法的第一個(gè)參數(shù)是表示方法名稱的一個(gè)符號(hào)和字符串。send 方法可以做到哪些您通常無(wú)法做到的事情?您可以使用 send 方法訪問一個(gè)類的私有方法。當(dāng)然,對(duì)于這是否是一個(gè)好特性仍然頗具爭(zhēng)議??匆幌?nbsp;清單 16 中的代碼。

清單 16. 訪問類私有方法

  1. irb(main):258:0class SendTest  
  2. irb(main):259:1private 
  3. irb(main):260:1> def hello  
  4. irb(main):261:2> puts "Saying Hello privately" 
  5. irb(main):262:2> end  
  6. irb(main):263:1> end  
  7. => nil  
  8. irb(main):264:0> y = SendTest.new 
  9. => #< SendTest:0x2cc52c0>  
  10. irb(main):265:0> y.hello  
  11. NoMethodError: private method `hello' called for #< SendTest:0x2cc52c0>  
  12.         from (irb):265 
  13. irb(main):266:0> y.send(:hello)  
  14. Saying Hello privately  
  15. => nil 

#p#

Throw 和 catch 并非表面那樣

如果您像我一樣具有 C++ 工作背景,且試圖編寫異常安全代碼,那么在看到 Ruby 有 throw 和 catch 關(guān)鍵詞時(shí)會(huì)開始感到異常親切。遺憾的是,throw 和 catch 在 Ruby 中的含義完全不同。

Ruby 通常使用 begin…rescue 塊處理異常。清單 17 提供了一個(gè)示例。

清單 17. Ruby 中的異常處理

  1. begin  
  2.   f = File.open("ruby.txt")  
  3.   # .. continue file processing  
  4. rescue ex => Exception  
  5.   # .. handle errors, if any  
  6. ensure  
  7.   f.close unless f.nil?  
  8.   # always execute the code in ensure block  
  9. end 

在 清單 17 中,如果在試圖打開文件時(shí)出錯(cuò)(可能是缺少文件或文件權(quán)限方面的問題),rescue 塊中的代碼會(huì)運(yùn)行。ensure 塊中的代碼始終運(yùn)行,不管是否有任何異常引發(fā)。注意,rescue 塊后面是否緊跟 ensure 塊是可選的。另外,如果必須顯式地拋出一個(gè)異常,那么語(yǔ)法是 raise <MyException>。如果您選擇擁有您自己的異常類,可能會(huì)希望從 Ruby 內(nèi)置的 Exception 類派生出相同的類,以利用現(xiàn)有方法。

Ruby 中的 catch 和 throw 代碼塊實(shí)際上不是異常處理:您可以使用 throw 修改程序流程。清單 18 顯示了一個(gè)使用 throw 和 catch的示例。

清單 18. Ruby 中的 Throw 和 catch

  1. irb(main):185:0catch :label do 
  2. irb(main):186:1* puts "This will print" 
  3. irb(main):187:1throw :label  
  4. irb(main):188:1> puts "This will not print" 
  5. irb(main):189:1> end  
  6. This will print  
  7. => nil 

在 清單 18 中,當(dāng)代碼運(yùn)行到 throw 語(yǔ)句時(shí),執(zhí)行會(huì)被中斷,解釋器開始尋找處理相應(yīng)符號(hào)的一個(gè) catch 塊。在 catch 塊結(jié)束的地方繼續(xù)執(zhí)行。查看 清單 19 中的 throw 和 catch 示例:注意,您可以輕松將 catch 和 throw 語(yǔ)句用于各個(gè)函數(shù)。

有些人甚至說(shuō),Ruby 中對(duì) catch 和 throw 的支持將 C goto 行為帶到一個(gè)全新的高度。鑒于函數(shù)可以有多個(gè)嵌套層,而 catch 塊可能在每一級(jí),goto 行為類比似乎有據(jù)可循。

 清單 19. Ruby 中的異常處理:嵌套的 catch 塊

  1. irb(main):190:0catch :label do 
  2. irb(main):191:1catch :label1 do 
  3. irb(main):192:2* puts "This will print" 
  4. irb(main):193:2throw :label  
  5. irb(main):194:2> puts "This won't print" 
  6. irb(main):195:2> end  
  7. irb(main):196:1> puts "Neither will this print" 
  8. irb(main):197:1> end  
  9. This will print  
  10. => nil 

#p#

Ruby 中的線程可以是綠色的

Ruby 版本 1.8.7 不支持真正的并發(fā)性。確實(shí)不支持。但是您會(huì)說(shuō),在 Ruby 中有 Thread 構(gòu)造函數(shù)。您說(shuō)的沒錯(cuò)。不過(guò)這個(gè)Thread.new 不會(huì)在您每次調(diào)用同一方法時(shí)生成一個(gè)真實(shí)的操作系統(tǒng)線程。Ruby 支持的是綠色線程:Ruby 解釋器使用單一操作系統(tǒng)線程來(lái)處理來(lái)自多個(gè)應(yīng)用程序級(jí)線程的工作負(fù)載。

當(dāng)某個(gè)線程等待一些輸入/輸出發(fā)生時(shí),這一 “綠色線程” 概念很有用,而且您可以輕松調(diào)度一個(gè)不同的 Ruby 線程來(lái)充分利用 CPU。但是這一構(gòu)造函數(shù)無(wú)法使用現(xiàn)代的多核 CPU(維基百科提供了一段內(nèi)容,很好地解釋了什么是綠色線程。參見 參考資料 獲取鏈接)。

最后這一個(gè)示例(參見 清單 20)證明了這一點(diǎn)。

清單 20. Ruby 中的多個(gè)線程

  1. #!/usr/bin/env ruby  
  2.    
  3. def func(id, count)  
  4.   i = 0;  
  5.   while (i < count)  
  6.     puts "Thread #{i} Time: #{Time.now}" 
  7.     sleep(1)  
  8.     i = i + 1 
  9.   end  
  10. end  
  11.    
  12. puts "Started at #{Time.now}" 
  13. thread1 = Thread.new{func(1100)}  
  14. thread2 = Thread.new{func(2100)}  
  15. thread3 = Thread.new{func(3100)}  
  16. thread4 = Thread.new{func(4100)}  
  17.    
  18. thread1.join  
  19. thread2.join  
  20. thread3.join  
  21. thread4.join  
  22. puts "Ending at #{Time.now}" 

假設(shè)您的 Linux® 或 UNIX® 機(jī)器上擁有 top 實(shí)用程序,在終端運(yùn)行代碼,獲取進(jìn)程 ID,然后再運(yùn)行 top –p <process id>。top啟動(dòng)后,按住 Shift-H 來(lái)列出運(yùn)行中線程的數(shù)量。您應(yīng)當(dāng)只能看到一個(gè)線程,確認(rèn)了這一點(diǎn):Ruby 1.8.7 中的并發(fā)性不過(guò)是個(gè)神話。

總的看來(lái),綠色線程沒有什么壞處。它們?cè)谥刎?fù)荷輸入/輸出密集型程序中仍然有用,更不用說(shuō)該方法可能是操作系統(tǒng)間最可移植的一個(gè)了。

結(jié)束語(yǔ)

本文涵蓋了以下多個(gè)方面:

● Ruby 中類層次結(jié)構(gòu)的概念

● 單例方法

● 解釋 self 關(guān)鍵詞和 method_missing 方法

● 異常

● 線程

盡管 Ruby 不乏特立獨(dú)行之處,但是使用它進(jìn)行編程還是挺有趣的,而且其以最少的代碼完成大量工作的能力還是很強(qiáng)大的。難怪 Twitter 這樣的大型應(yīng)用程序會(huì)使用 Ruby 來(lái)駕馭其真正的潛力。祝您有個(gè)快樂的 Ruby 編程體驗(yàn)!

參考資料

學(xué)習(xí)

● 閱讀 Programming Ruby: The Pragmatic Programmers’ Guide(Dave Thomas,Chad Fowler 和 Andy Hunt;第二版),這是一本 Ruby 必讀書籍,也就是廣為人知的 Pickaxe 圖書。

● 查閱另一個(gè)寶貴的 Ruby 資源 The Ruby Programming Language [Yukihiro "Matz" Matsumoto(Ruby 的創(chuàng)建者)和 David Flanagan,O'Reilly,2008 年]。

● 訪問 To Ruby From C and C++,這是一個(gè)面向希望學(xué)習(xí) Ruby 的 C/C++ 程序員的一個(gè)不錯(cuò)站點(diǎn)

● 在維基百科上了解更多有關(guān) 綠色線程 的解釋信息。

●  IBM Rational Twitter。

● 觀看 演示如何用 WebSphere Studio 快速開發(fā)Web Services,包括面向初學(xué)者的產(chǎn)品安裝和設(shè)置演示,以及為經(jīng)驗(yàn)豐富的開發(fā)人員提供的高級(jí)功能。

● 在 developerWorks Linux 專區(qū) 尋找為 Linux 開發(fā)人員(包括 Linux 新手入門)準(zhǔn)備的更多參考資料,查閱我們 最受歡迎的文章和教程。

● 在 developerWorks 上查閱所有 Linux 技巧 和 Linux 教程。

● 隨時(shí)關(guān)注 developerWorks 技術(shù)活動(dòng)網(wǎng)絡(luò)廣播。

●  訪問 developerWorks Open source 專區(qū)獲得豐富的 how-to 信息、工具和項(xiàng)目更新以及最受歡迎的文章和教程,幫助您用開放源碼技術(shù)進(jìn)行開發(fā),并將它們與 IBM 產(chǎn)品結(jié)合使用。

獲得產(chǎn)品和技術(shù)

● 使用 IBM 產(chǎn)品評(píng)估試用版軟件(可以通過(guò)下載獲得),并使用專門面向開發(fā)人員的軟件改進(jìn)您的下一個(gè)開源開發(fā)項(xiàng)目。

討論

● 加入 developerWorks 中文社區(qū),developerWorks 社區(qū)是一個(gè)面向全球 IT 專業(yè)人員,可以提供博客、書簽、wiki、群組、聯(lián)系、共享和協(xié)作等社區(qū)功能的專業(yè)社交網(wǎng)絡(luò)社區(qū)。

● 加入 IBM 軟件下載與技術(shù)交流群組,參與在線交流。

原文鏈接:http://www.ibm.com/developerworks/cn/opensource/os-sixrubyfeatures/index.html

責(zé)任編輯:林師授 來(lái)源: developerWorks
相關(guān)推薦

2012-06-15 11:30:55

ibmdw

2013-05-27 11:30:41

IPv6IP協(xié)議IPv6應(yīng)用

2021-03-16 10:40:40

人工智能人工智能運(yùn)維AIOps

2019-08-05 07:55:57

2019-10-16 16:56:16

小程序BAT微信

2023-11-09 09:02:26

TypeScriptas const

2016-11-07 20:34:31

高層管理者大數(shù)據(jù)誤解

2020-05-19 11:40:17

網(wǎng)絡(luò)安全 信息安全技術(shù)

2017-08-31 14:25:34

前端JavascriptES6

2020-07-01 07:58:20

ES6JavaScript開發(fā)

2009-12-17 17:04:09

Ruby函數(shù)參數(shù)傳遞

2013-02-25 14:02:07

RubyWeb

2020-01-15 11:01:01

端點(diǎn)安全端點(diǎn)防護(hù)EDR

2009-12-18 11:03:45

Ruby watir環(huán)

2018-11-15 14:05:24

MongoDB數(shù)據(jù)庫(kù)事務(wù)

2009-06-21 13:28:10

2010-01-15 10:26:08

2010-10-29 11:01:11

簡(jiǎn)歷

2020-07-10 08:15:19

遞歸算法函數(shù)

2018-04-12 16:02:30

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)