posts - 403, comments - 310, trackbacks - 0, articles - 7
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          Ruby 入門筆記 (1)

          Posted on 2007-06-14 14:02 ZelluX 閱讀(341) 評論(0)  編輯  收藏 所屬分類: Scripting
          先把各種熱門的東西都走馬觀花地看一遍,呵呵。
          看的是Everyday Scripting with Ruby,風格和In Action系列差不多,大量的實例。
          現在學Ruby的主要目的也是everyday scripting,方便數據處理、生成,文件批處理等,RoR之類的暫時不考慮。

          1. String.inspect 方法
          文檔中的說法是
               str.inspect   => string

               Returns a printable version of _str_, with special characters
               escaped.

                  str = "hello"
                  str[3] = 8
                  str.inspect       #=> "hel\010o"

          具體情況試試 myString.inspect.inspect....就能了解一點了

          2. Arrays.each 和 Arrays.collect
          for_each方法

          irb(main):007:0> [1, 2, 3].each do | element |
          irb(main):008:1*   puts element
          irb(main):009:1> end
          1
          2
          3
          => [1, 2, 3]

          后者與前者的不同之處在于,在處理數據的同時,每次處理的返回結果都會保存到一個新的數組中返回。
          irb(main):036:0> newarray = ["aBC", "B"].collect do |e|
          irb(main):037:1* e.downcase
          irb(main):038:1> end
          => ["abc", "b"]

          3. Messages and Methods
          It can be hard to remember the difference between messages and methods. A message is a request sent from some sender object. When the receiver object receives the message, it looks to see whether it has a method with the same name. If so, the Ruby code within the method is run, and the results are returned to the sender. The message is the request; the method fulfills it.
          呃,還是沒有感性認識。

          4. Delimiting Blocks
          塊的兩種表示方式:
          array.each do | element |
              puts element
          end
          array.each { | element |
              puts element
          }
          通常使用第一種,但可以用一行寫成的情況也可以使用第二種:
          array.each { | element | puts element }


          主站蜘蛛池模板: 神农架林区| 泸定县| 巴东县| 宁安市| 江安县| 阜宁县| 陕西省| 宁南县| 赫章县| 商洛市| 沧州市| 教育| 陕西省| 晋宁县| 永嘉县| 闸北区| 商都县| 襄垣县| 芒康县| 从江县| 新干县| 峨山| 忻城县| 清水河县| 静乐县| 通山县| 深泽县| 农安县| 红桥区| 庆元县| 镇雄县| 岐山县| 顺平县| 东丰县| 山阴县| 襄垣县| 津南区| 庄浪县| 台南市| 勃利县| 花莲县|