• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            馭風萬里無垠

            ruby學習筆記《二》

            ruby的blocks和closure特性明顯有別于其它的語言,其closure本身是real closure,所綁定的context是共享的而非copy,其設計思路和lisp的相同;blocks本身則可以用于實現closure。二者的關系如下所述 (來源

            Yukihiro Matsumoto: You can reconvert a closure back into a block, so a closure can be used anywhere a block can be used. Often, closures are used to store the status of a block into an instance variable, because once you convert a block into a closure, it is an object that can by referenced by a variable. And of course closures can be used like they are used in other languages, such as passing around the object to customize behavior of methods. If you want to pass some code to customize a method, you can of course just pass a block. But if you want to pass the same code to more than two methods -- this is a very rare case, but if you really want to do that -- you can convert the block into a closure, and pass that same closure object to multiple methods.
            • 7種結構

            • block方式3種
            • 隱式傳入,內部用yield調用
              
            def thrice
              
            yield
              
            yield
              
            yield
            end

            x
            =1
            thrice {x
            +=2}
            • &block參數傳入,內部直接操作&block
            def six_times(&block)
              thrice(
            &block)
              thrice(
            &block)
            end
             
            = 4
            six_times { x 
            += 10 }
            • &block傳入,保存block為變量,然后調用block.call
            def save_for_later(&b)
              @saved 
            = b  # Note: no ampersand! This turns a block into a closure of sorts.
            end
             
            save_for_later { puts 
            "Hello!" }
            puts 
            "Deferred execution of a block:"
            @saved.call
            @saved.call
            這里的saved保存為main對象的一個成員,后邊實現延遲調用。
            • Proc.new/proc
            @saved_proc_new = Proc.new { puts "I'm declared with Proc.new." }
            @saved_proc 
            = proc { puts "I'm declared with proc." }

            @saved_proc_new.call
            @saved_proc.call
            • lambda
            @saved_lambda = lambda { puts "I'm declared with lambda." }
            @saved_lambda.call
            • method
            def some_method
              puts 
            "I'm declared as a method."
            end
            @method_as_closure 
            = method(:some_method)

            • 特點和差異

              • return行為

            當對應的block里邊包含return的時候,上述7中方式有些許的不同:
            • lambda/method表現出真正的closure行為,僅僅返回closure本身;外部調用控制流不受影響,繼續yield或者call的下一語句執行
            • 其它幾種會跳出外部調用者的控制流,即return出調用者,yield/call之后的也不會再執行,直接跳出到最近的end外


            • arity - 參數個數校驗

            對于調用點的參數檢查,呈現如下行為:
            • lambda/method嚴格校驗參數的個數,如果不匹配回拋出異常
            • 其它幾個不檢查參數個數


            • 總結

            lambda/method方式呈現完備的closure行為,return之后繼續下一流程,對于實際傳入參數個數會在調用點檢查;proc/blocks方式在return的時候直接返回了外部的函數或者block,對于傳入的參數個數也沒有執行檢查。

            參考:http://innig.net/software/ruby/closures-in-ruby
            以上結論驗證于最新的ruby1.9.3,和鏈接中的結論有些許不同;ruby1.8中的proc函數可能有不同的行為。

            posted on 2012-02-19 19:29 skyscribe 閱讀(457) 評論(0)  編輯 收藏 引用 所屬分類: Misc

            <2025年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            導航

            統計

            常用鏈接

            留言簿(3)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            欧洲性大片xxxxx久久久| 久久精品国产亚洲AV无码偷窥| 2021少妇久久久久久久久久| 久久久久99精品成人片欧美| 国产精品美女久久久| 天天影视色香欲综合久久| 精品伊人久久大线蕉色首页| 精品久久久久久亚洲| 日韩一区二区三区视频久久| 久久这里只有精品18| 久久精品成人一区二区三区| 午夜精品久久久久久毛片| 久久99精品久久久久久久不卡| 久久九色综合九色99伊人| 久久综合狠狠综合久久| 青青久久精品国产免费看 | www.久久99| 一级a性色生活片久久无少妇一级婬片免费放 | 色综合久久久久网| 久久综合九色综合久99| 国产麻豆精品久久一二三| 伊人久久免费视频| 亚洲国产精品一区二区久久hs| 久久福利片| 久久99久久成人免费播放| 国内精品伊人久久久久av一坑| 久久天天躁夜夜躁狠狠躁2022| 精品久久久久久无码免费| 久久免费高清视频| 久久久亚洲欧洲日产国码aⅴ| 色综合久久88色综合天天 | 99久久无码一区人妻| 久久综合丁香激情久久| 国产精品久久久久久久久鸭| 久久人人妻人人爽人人爽| 成人综合久久精品色婷婷| 亚洲人成无码www久久久| 日本亚洲色大成网站WWW久久| 久久久91人妻无码精品蜜桃HD| 久久99精品国产麻豆蜜芽| 开心久久婷婷综合中文字幕|