举例讲解Ruby中迭代器Iterator的用法

 更新时间:2015年05月11日 15:08:54   作者:pringwq  
这篇文章主要介绍了举例讲解Ruby中迭代器Iterator的用法,是Ruby学习进阶中的重要知识,需要的朋友可以参考下

Iterator
定义

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

  •         Block 一般是跟着 method 出现的, 并且 block 中的代码不一定会执行
  •         如果 method 中有 yield, 那么它的block 中的代码会被执行
  •         Block 可以接收参数,和返回 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 系统的交互

Iterators 不仅仅能够访问 Array 和 Hash 中的数据, 和可以和 I/O 系统交互

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


相关文章

  • Rails中遇到错误跳转到统一提示错误页的方法

    Rails中遇到错误跳转到统一提示错误页的方法

    这篇文章主要介绍了Rails中遇到错误跳转到统一提示错误页的方法,本文在ApplicationController中添加捕捉错误并跳转到统一提示页面,需要的朋友可以参考下
    2015-01-01
  • ruby实现的文件自删除代码分享

    ruby实现的文件自删除代码分享

    这篇文章主要介绍了ruby自删除代码分享,本文代码适用Linux系统,Windows系统需要自测一下哈,需要的朋友可以参考下
    2015-01-01
  • Ruby单元测试框架TestUnit的替代者MiniTest介绍

    Ruby单元测试框架TestUnit的替代者MiniTest介绍

    这篇文章主要介绍了Ruby单元测试框架TestUnit的替代者MiniTest介绍,本文先是对比了TestUnit、MiniTest的优劣,然后给出了MiniTest的使用示例,需要的朋友可以参考下
    2015-03-03
  • ruby使用restclient上传服务器本地文件示例

    ruby使用restclient上传服务器本地文件示例

    这篇文章主要介绍了ruby使用restclient上传服务器本地文件示例,需要的朋友可以参考下
    2014-05-05
  • Ruby on Rails基础之新建项目

    Ruby on Rails基础之新建项目

    Ruby on Rails 是一个可以使你开发,部署,维护 web 应用程序变得简单的框架。下面我们就来看看如何简单便捷的使用这一框架,本系列文章将一一为大家揭秘
    2016-02-02
  • Ruby中的钩子方法详解

    Ruby中的钩子方法详解

    这篇文章主要介绍了Ruby中的钩子方法详解,本文讲解了什么是钩子方法、included、Devise中的 included、extended、ActiveRecord中的 extended、prepended、inherited等内容,需要的朋友可以参考下
    2015-05-05
  • Luhn算法学习及其Ruby版实现代码示例

    Luhn算法学习及其Ruby版实现代码示例

    Luhn算法主要北用来进行数字验证,尤其是卡号身份证号等,这里我们就来看一下Luhn算法学习及其Ruby版实现代码示例:
    2016-05-05
  • ruby 简单例子

    ruby 简单例子

    ruby 简单例子...
    2007-11-11
  • ruby中的双等号==问题详解

    ruby中的双等号==问题详解

    Ruby里面有4种比较方法,equal?, eql?, ==, ===,而且在不同的类里面表现的很不一样。在使用的时候也特别容易搞糊涂。 本文先给大家讲述一下==号的用法及使用中应该注意的地方
    2016-02-02
  • Ruby中的return、break、next详解

    Ruby中的return、break、next详解

    这篇文章主要介绍了Ruby中的return、break、next详解,这三个关键字一般都用来作为语句中断或返回功能,本文就详细介绍了相关内容,需要的朋友可以参考下
    2015-05-05

最新评论