<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    ruby學習筆記(4)

    本節課主要4部分內容如下:Ranges、Blocks and Procs、Random Numbers、Read/Write Text Files
    4.1 Ranges
        1)序列的定義:有起始值、結束值并且能夠獲取連續值的數據結構
        2)使用.. 或者 ...聲明序列,
            注意:..包含最大值,...不包含最大值
            如 (1..3)  #結果是1,2,3
               (
    13)  #結果是1,2
        3)ruby使用Range對象處理序列,(1..3)就作為一個Range對象.Range.to_a方法將Range轉換為Array
           如 (1..10).to_a -> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
        4)可以使用===判斷序列中是否有指定的值
        如
            (1..10) === 5       -> true 
            (1..10) === 15      -> false 
            (1..10) === 3.14159 -> true 
            ('a'..'j') === 'c'  -> true 
            ('a'..'j') === 'z'  -> false

    4.2 Blocks and Procs
        1)block是一組代碼段(grouping statements),它根據上下文的語義執行.
        2)block 單行定義用{},多行定義使用do end
        3)可以使用yield在方法中調用多次相關的block
        如
            def call_block 
                puts 
    'Start of method' 
                yield    #在方法中調用block{puts 
    'In the block'}
                yield 
                puts 
    'End of method' 
            end 
            call_block {puts 
    'In the block'}    
        4)code Block的返回值和方法類似,將最后一行的表達式作為返回值
        5)block不是對象,但可以通過kernel的lambda方法轉換成Proc對象,
            然后可以通過Proc的call調用block
            prc = lambda {puts 'Hello'}  #將block{puts 'Hello'}轉換成對象
            prc.call                                #調用Proc對象的call方法執行block
        注意:block有點回調函數的概念,它根據上下文執行具體的code
       
    4.3 Random Numbers
        1)ruby使用rand產生隨機數
        2)rand(x)將生成大于0小于5的隨機整數
      
    4.4    Read/Write Text Files
        1) File.open 打開文件,可以指定打開方式:'r' Read-only、(default); 'r+' Read/Write、'w' Write-only
        2) 使用Find module來獲取文件夾下的內容 Find.find("./") #獲取當前路徑下的所有文件
        3) Ruby Object Serialization
            使用Marshal.dump Saving an object and some or all of its components
            使用Marshal.load econstitute the object

    4.5 總結
        * Sequences have a start point, an end point, and a way to produce successive values in the sequence. In Ruby, these sequences are created using the ".." and "" range operators.
        
    * The two dot form creates an inclusive range, and the three-dot form creates a range that excludes the specified high value.
        
    * In Ruby, the sequence 1..100000 is held as a Range object containing references to two Fixnum objects.
        
    * The .to_a method converts a Range to an Array.
        
    * Another use of the versatile range is as an interval test: seeing if some value falls within the interval represented by the range. We do this using ===, the case equality operator.
        
    * Ranges are not limited to integers or numbers. The beginning and end of a range may be any Ruby object.
        
    * IMPORTANT: Ruby Code blocks are chunks of code between braces or between do- end that you can associate with method invocations.
        
    * Code blocks may appear only in the source adjacent to a method call; the block is written starting on the same line as the method call's last parameter (or the closing parenthesis of the parameter list).
     The code in the block is not executed at the time it is encountered. Instead, Ruby remembers the context in which the block appears (the local variables, the current object, and so on) and then enters the method.

        * The Ruby standard is to use braces for single-line blocks and do- end for multi-line blocks. Keep in mind that the braces syntax has a higher precedence than the do..end syntax.
        
    * Inside a method, you can call a Ruby block using the yield keyword with a value.
        
    * You can provide parameters to the call to yield: these will be passed to the block. Within the block, you list the names of the arguments to receive the parameters between vertical bars (|).
        
    * Blocks are not objects, but they can be converted into objects of class Proc. This can be done by calling the lambda method of the module Kernel.
        
    * Remember you cannot pass methods into other methods (but you can pass procs into methods), and methods cannot return other methods (but they can return procs).
        
    * The method to get a randomly chosen number in Ruby is rand.
        
    * If you call rand, you'll get a float greater than or equal to 0.0 and less than 1.0. If you give it an integer parameter (by calling rand(5) ), you will get an integer value greater than or equal to 0 and less than 5.
        * The File.open method can open a file in different modes like 'r' Read-only, starts at beginning of file (default); 'r+' Read/Write, starts at beginning of file; 'w' Write-only, truncates existing file to zero length or creates a new file for writing.
        
    * File.open opens a new File if there is no associated block. If the optional block is given, it will be passed file as an argument, and the file will automatically be closed when the block terminates.
        
    * Always close a file that you open. In the case of a file open for writing, this is very important and can actually prevent lost data.
        
    * The seek method of class IO, seeks to a given offset an Integer (first parameter of method) in the stream according to the value of second parameter in the method. The second parameter can be IO::SEEK_CUR - Seeks to first integer number parameter plus current position; IO::SEEK_END - Seeks to first integer number parameter plus end of stream (you probably want a negative value for first integer number parameter); IO::SEEK_SET - Seeks to the absolute location given by first integer number parameter.

    4.6 原文鏈接

    posted on 2007-10-23 17:59 想飛就飛 閱讀(3225) 評論(1)  編輯  收藏 所屬分類: ROR

    評論

    # re: ruby學習筆記(4) 2008-01-27 12:35 張曉婷

    我因為是剛開始學習用c++編程,我在網上找到了這里,問一個很蠢的問題可以嗎?怎么樣在code Blocks中用平方根?我的郵箱::zxtjlu@yahoo.ca,
    thank you for your time.  回復  更多評論   

    公告


    導航

    <2007年10月>
    30123456
    78910111213
    14151617181920
    21222324252627
    28293031123
    45678910

    統計

    常用鏈接

    留言簿(13)

    我參與的團隊

    隨筆分類(69)

    隨筆檔案(68)

    最新隨筆

    搜索

    積分與排名

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: AAA日本高清在线播放免费观看| 亚洲av乱码一区二区三区按摩| a高清免费毛片久久| 四虎影院免费在线播放| 精品亚洲AV无码一区二区三区| 亚洲人成免费网站| 亚洲综合久久1区2区3区| 亚洲免费黄色网址| 亚洲黄色免费观看| 国产免费女女脚奴视频网| 亚洲国产成AV人天堂无码| 久久精品免费全国观看国产| 亚洲伊人久久大香线焦| 最近免费中文字幕4| 免费精品国自产拍在线播放| 亚洲人成电影在线播放| 在线看片免费人成视频久网下载| 亚洲国产AV无码专区亚洲AV| 99re在线视频免费观看| 亚洲一区二区三区免费视频 | 亚洲av无码国产精品色午夜字幕| 插鸡网站在线播放免费观看| 亚洲成AV人片在线观看| 曰批视频免费40分钟试看天天| 亚洲AV无码精品蜜桃| 国产成人在线观看免费网站| 在线播放国产不卡免费视频| 亚洲AV综合色区无码一区爱AV| 亚洲成人免费在线观看| 校园亚洲春色另类小说合集 | ww在线观视频免费观看| 亚洲av无码有乱码在线观看| 国产日韩成人亚洲丁香婷婷| 在线观看永久免费| 曰批免费视频播放免费| 图图资源网亚洲综合网站| 最近最好的中文字幕2019免费 | 亚洲欧洲中文日韩av乱码| 午夜老司机永久免费看片| 亚洲人成色4444在线观看| 亚洲另类激情综合偷自拍图|