最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

舉例講解Ruby中迭代器Iterator的用法

 更新時間:2015年05月11日 15:08:54   作者:pringwq  
這篇文章主要介紹了舉例講解Ruby中迭代器Iterator的用法,是Ruby學(xué)習(xí)進(jìn)階中的重要知識,需要的朋友可以參考下

Iterator
定義

A Ruby iterator is simple a method that can invoke a block of code.

  •         Block 一般是跟著 method 出現(xiàn)的, 并且 block 中的代碼不一定會執(zhí)行
  •         如果 method 中有 yield, 那么它的block 中的代碼會被執(zhí)行
  •         Block 可以接收參數(shù),和返回 value
def two_times
  yield
  yield
end
two_times { puts "Hello" }
# Hello
# Hello

def fib_up_to(max)
 i1, i2 = 1. 1
 while i1 <= max
   yield i1
   i1, i2 = i2, i1 + i2
 end
end

fib_up_to(1000) { |f| print f, " " }

# 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987

    上面代碼中的 yield 之后的 i1 會作為 parameter 傳入到 block 中, 賦值給 block 的 argument f。
    Block 中可以有多個 arguments.

常見的 iterator
each

each is probable the simplest iterator - all it does is yield successive elements of its collection.

[1, 3, 5, 7, 9].each { |i| puts i }

# 1 
# 3
# 5
# 7
# 9

find

A blocl may also return a value to the method. The value of the last expression evaluated in the block is passed back to the method as the value of the yield.

class Array
 def find
  each do |value|
    return value if yield(value)
  end
 end
end

[1,3,4,7,9].find { |v| V*V > 30 } # => 7

collect (also known as map)

Which takes each element from the collection and passes it to the block. The results returned by the block are used to construct a new array

["H", "A", "L"].collect { |x| x.succ } # => ["I", "B", "M"]

inject

The inject method lets you accumulate a value across the members of a collection.

[1,3,5,7].inject { |sum, element| sum + element } # => 16

# sum = 1, element = 3
# sum = 4, element = 5
# sum = 9, element = 7
# sum = 16

[1,3,5,6].inject { |product, element| product*element } # => 105

If inject is called with no parameter, it uses the first element of the collections as the initial value and starts the iteration with the second value.

上面代碼的另一種簡便寫法:

[1,3,5,7].inject(:+) # => 16
[1,3,5,7]/inject(:*) # => 105

Iterator 和 I/O 系統(tǒng)的交互

Iterators 不僅僅能夠訪問 Array 和 Hash 中的數(shù)據(jù), 和可以和 I/O 系統(tǒng)交互

f = File.open("testfile")
f.each do |line|
 puts "The line is: #{line}"
end
f.close

produces:
The line is: This is line one
The line is: This is line two
The line is: This is line three


相關(guān)文章

  • Rails中遇到錯誤跳轉(zhuǎn)到統(tǒng)一提示錯誤頁的方法

    Rails中遇到錯誤跳轉(zhuǎn)到統(tǒng)一提示錯誤頁的方法

    這篇文章主要介紹了Rails中遇到錯誤跳轉(zhuǎn)到統(tǒng)一提示錯誤頁的方法,本文在ApplicationController中添加捕捉錯誤并跳轉(zhuǎn)到統(tǒng)一提示頁面,需要的朋友可以參考下
    2015-01-01
  • ruby實現(xiàn)的文件自刪除代碼分享

    ruby實現(xiàn)的文件自刪除代碼分享

    這篇文章主要介紹了ruby自刪除代碼分享,本文代碼適用Linux系統(tǒng),Windows系統(tǒng)需要自測一下哈,需要的朋友可以參考下
    2015-01-01
  • Ruby單元測試框架TestUnit的替代者M(jìn)iniTest介紹

    Ruby單元測試框架TestUnit的替代者M(jìn)iniTest介紹

    這篇文章主要介紹了Ruby單元測試框架TestUnit的替代者M(jìn)iniTest介紹,本文先是對比了TestUnit、MiniTest的優(yōu)劣,然后給出了MiniTest的使用示例,需要的朋友可以參考下
    2015-03-03
  • ruby使用restclient上傳服務(wù)器本地文件示例

    ruby使用restclient上傳服務(wù)器本地文件示例

    這篇文章主要介紹了ruby使用restclient上傳服務(wù)器本地文件示例,需要的朋友可以參考下
    2014-05-05
  • Ruby on Rails基礎(chǔ)之新建項目

    Ruby on Rails基礎(chǔ)之新建項目

    Ruby on Rails 是一個可以使你開發(fā),部署,維護(hù) web 應(yīng)用程序變得簡單的框架。下面我們就來看看如何簡單便捷的使用這一框架,本系列文章將一一為大家揭秘
    2016-02-02
  • Ruby中的鉤子方法詳解

    Ruby中的鉤子方法詳解

    這篇文章主要介紹了Ruby中的鉤子方法詳解,本文講解了什么是鉤子方法、included、Devise中的 included、extended、ActiveRecord中的 extended、prepended、inherited等內(nèi)容,需要的朋友可以參考下
    2015-05-05
  • Luhn算法學(xué)習(xí)及其Ruby版實現(xiàn)代碼示例

    Luhn算法學(xué)習(xí)及其Ruby版實現(xiàn)代碼示例

    Luhn算法主要北用來進(jìn)行數(shù)字驗證,尤其是卡號身份證號等,這里我們就來看一下Luhn算法學(xué)習(xí)及其Ruby版實現(xiàn)代碼示例:
    2016-05-05
  • ruby 簡單例子

    ruby 簡單例子

    ruby 簡單例子...
    2007-11-11
  • ruby中的雙等號==問題詳解

    ruby中的雙等號==問題詳解

    Ruby里面有4種比較方法,equal?, eql?, ==, ===,而且在不同的類里面表現(xiàn)的很不一樣。在使用的時候也特別容易搞糊涂。 本文先給大家講述一下==號的用法及使用中應(yīng)該注意的地方
    2016-02-02
  • Ruby中的return、break、next詳解

    Ruby中的return、break、next詳解

    這篇文章主要介紹了Ruby中的return、break、next詳解,這三個關(guān)鍵字一般都用來作為語句中斷或返回功能,本文就詳細(xì)介紹了相關(guān)內(nèi)容,需要的朋友可以參考下
    2015-05-05

最新評論

平武县| 禹城市| 廉江市| 仙桃市| 成武县| 温宿县| 正阳县| 成安县| 冀州市| 本溪| 济源市| 黔西县| 资兴市| 凉山| 玉林市| 广宗县| 怀来县| 濮阳市| 通城县| 古浪县| 定南县| 保山市| 邢台县| 景洪市| 鲜城| 临沭县| 泌阳县| 曲靖市| 临桂县| 甘洛县| 揭西县| 枞阳县| 喀喇| 韶山市| 崇明县| 冷水江市| 清水河县| 海兴县| 额尔古纳市| 镇原县| 南乐县|