• <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,其設(shè)計思路和lisp的相同;blocks本身則可以用于實現(xiàn)closure。二者的關(guān)系如下所述 (來源

            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種結(jié)構(gòu)

            • block方式3種
            • 隱式傳入,內(nèi)部用yield調(diào)用
              
            def thrice
              
            yield
              
            yield
              
            yield
            end

            x
            =1
            thrice {x
            +=2}
            • &block參數(shù)傳入,內(nèi)部直接操作&block
            def six_times(&block)
              thrice(
            &block)
              thrice(
            &block)
            end
             
            = 4
            six_times { x 
            += 10 }
            • &block傳入,保存block為變量,然后調(diào)用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對象的一個成員,后邊實現(xiàn)延遲調(diào)用。
            • 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行為

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


            • arity - 參數(shù)個數(shù)校驗

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


            • 總結(jié)

            lambda/method方式呈現(xiàn)完備的closure行為,return之后繼續(xù)下一流程,對于實際傳入?yún)?shù)個數(shù)會在調(diào)用點檢查;proc/blocks方式在return的時候直接返回了外部的函數(shù)或者block,對于傳入的參數(shù)個數(shù)也沒有執(zhí)行檢查。

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

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

            <2025年7月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            導航

            統(tǒng)計

            常用鏈接

            留言簿(3)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            精品久久久久中文字| 久久香综合精品久久伊人| 久久久久人妻一区精品色| 中文国产成人精品久久不卡| 亚洲AV乱码久久精品蜜桃| 久久久久亚洲av无码专区喷水| 久久综合中文字幕| 久久综合九色欧美综合狠狠| 久久亚洲精品中文字幕| 久久综合九色欧美综合狠狠| 久久久久久午夜成人影院| 国产精品伊人久久伊人电影| 午夜不卡久久精品无码免费| 久久影院久久香蕉国产线看观看| 97精品依人久久久大香线蕉97| 精品无码久久久久久久动漫| 久久精品夜夜夜夜夜久久| 手机看片久久高清国产日韩| 国产精品久久久久久一区二区三区| 亚洲伊人久久成综合人影院 | 久久久婷婷五月亚洲97号色| 狠狠人妻久久久久久综合蜜桃| 潮喷大喷水系列无码久久精品 | 精品蜜臀久久久久99网站| 久久精品一区二区影院| 99久久精品国内| 午夜精品久久久久久中宇| 香港aa三级久久三级老师2021国产三级精品三级在 | 国产日韩久久免费影院| 精品久久久久久久久午夜福利| 人妻无码精品久久亚瑟影视| 色8激情欧美成人久久综合电| 久久久黄片| 久久久久久久国产免费看| 国产成人综合久久精品尤物| 国产精品一久久香蕉国产线看观看| 97精品伊人久久久大香线蕉| 久久精品国产亚洲AV蜜臀色欲 | 99久久精品影院老鸭窝| 国产三级久久久精品麻豆三级| 韩国无遮挡三级久久|