`
simohayha
  • 浏览: 1395664 次
  • 性别: Icon_minigender_1
  • 来自: 火星
社区版块
存档分类
最新评论

ruby way之Enumerables

    博客分类:
  • ruby
阅读更多
Enumerable 模块是一个很重要的模块,ruby中的很多类都有mix这个模块,比如array,hash等等。因此如果你想要自己定义一个collection,则不要继承一个存在的collection,而是应该mix Enumerable 模块。

数组是最常用,最有代表性的mix Enumerable模块的一个集合。因此下面的都会默认用数组来举例子

1 inject方法

先看一个很简单的例子:

nums = [3,5,7,9,11,13]
sum = nums.inject(0) {|x,n| x+n }


这个结果就是 将nums的元素一次累加起来付给x,然后最终返回x.而inject的参数0的意思是x的初始值是0.

于是上面的代码其实也就等同于下面的代码:

sum = 0
nums.each {|n| sum += n }
puts sum


如果你忽略了inject的第一个参数,则它将会把所要迭代的数组的第一个元素作为那个累加值的默认值,然后从下一个元素开始迭代:

sum = nums.inject {|x,n| x+n }

#和下面的代码是等价的

sum = nums[0]
nums[1..-1].each {|n| sum += n }


下面可以看一下这个比较复杂的例子:

words = %w[ alpha beta gamma delta epsilon eta theta ]
longest_word=words.inject do |best,w|
  w.length > best.length ? w : best
end
puts longest_word


这个例子的结果也就是words得到数组里面最长的那个元素 。

2 使用量词符.

使用any?和all?方法能够很容易测试一个集合:

nums = [1,3,5,8,9]

# 这些元素有一个奇数吗?
flag1 = nums.any? {|x| x % 2 == 0 }    # true

#这些元素都是偶数吗?
flag2 = nums.all? {|x| x % 2 == 0 }    # false


如果没有block参数时呢:

flag1 = list.all?   # list  包含no falses 或者nils
flag1 = list.any?   # list contains at least one true value (non-nil
                    #   or non-false)


3 partition 方法

简而言之,partition 方法就是用来分组的,也就是说,它可以将一个数组根据一定的规则分为不同的组.

nums = [1, 2, 3, 4, 5, 6, 7, 8, 9]

odd_even = nums.partition {|x| x % 2 == 1 }
# [[1,3,5,7,9],[2,3,4,6,8]]

under5 = nums.partition {|x| x < 5 }
# [[1,2,3,4],[5,6,7,8,9]]

squares = nums.partition {|x| Math.sqrt(x).to_i**2 == x }
# [[1,4,9],[2,3,5,6,7,8]]


代码很容易懂,也就是partition将数组中的每一个元素都传进它的block,然后用返回值来进行分组:

4 分组迭代

以前我们所介绍的迭代都是每次迭代一个元素,如果我们想要迭代一个组的时候,我们可以使用each_slice:

require 'enumerator'

arr = [1,2,3,4,5,6,7,8,9,10]
arr.each_slice(3) do |triple|
  puts triple.join(",")
end
#这里传入的3,也就意味着,每次传进去三个元素.
# Output:
# 1,2,3
# 4,5,6
# 7,8,9
# 10


这里还有一个each_cons 方法,这个是从lisp得来的,看下面的代码:

require 'enumerator'

arr = [1,2,3,4,5,6,7,8,9,10]
arr.each_cons(3) do |triple|
  puts triple.join(",")
end

# Output:
# 1,2,3
# 2,3,4
# 3,4,5
# 4,5,6
# 5,6,7
# 6,7,8
# 7,8,9
# 8,9,10



5 使用Enumerator对象

Enumerator 一般是作为一个包装器,将一个iterator 方法转换成一个完全的Enumerable.当完成转换之后,这个对象将会有很多方法可以使用.

看下面的代码:

require 'enumerator'

class Foo
  def every
    yield 3
    yield 2
    yield 1
    yield 4
  end
end

foo = Foo.new

# 传递一个对象和它的iterator 名字
enum = Enumerable::Enumerator.new(foo,:every)

enum.each {|x| p x }     # Print out the items
array = enum.to_a        # [3,2,1,4]
sorted = enum.sort       # [1,2,3,4]


其实上面的那些转换代码和下面的代码是等价的:

enum = []
foo.every {|x| enum << x }


我们下面还有另外一方法,来转换一个对象到Enumerable.

如果enumerator 被require,Object 将会有一个enum_for 方法,因此我们的转换方式将变为:

enum = foo.enum_for(:every)


对应于each_slice 和each_cons,当require了enumerator之后对象还会有enum_slice和enum_cons方法,这边要注意Enumerator.new方法,还能多加参数,它的后面的参数也就是所传进去的方法的参数:

array = [5,3,1,2]

discrete = array.enum_slice(2)
# Same as: Enumerable::Enumerator.new(array,:each_slice,2)

overlap  = array.enum_cons(2)
# Same as: Enumerable::Enumerator.new(array,:each_cons,2)

discrete.each {|x| puts x.join(",") }
# Output:
# 5,3
# 1,2

overlap.each {|x| puts x.join(",") }
# Output:
# 5,3
# 3,1
# 1,2


6使用Generator 对象


一般ruby的迭代器都是内置的,迭代器通过它的block来操作逻辑.

ruby还有一个外部的迭代器,这种迭代器它能够提供给你数据,让你来操作,比如IO中的getline函数。

generator 库能够转换内置的迭代器到外部的迭代器,它提供一个类似于IO的接口,这个接口含有next, rewind, 和 end?这样的方法:

require 'generator'

array = [7,8,9,10,11,12]

gen = Generator.new(array)

what  = gen.current    # 7
where = gen.index      # 0  (same as pos)

while gen.current < 11 and gen.next? 
  puts "#{gen.index},#{gen.next}"
end

#0,7 1,8 2,9 3,10

puts gen.current       # 11
puts gen.next          # 11
puts gen.index         # 5    (index same as pos)
puts gen.next?         # true   (next? same as end?)
puts gen.next          # 12
puts gen.next?         # false



这里还有一个prev 方法,来得到上一个迭代的元素,而rewind 方法,则是重新将迭代的位置设置到开始的位置.

这边要注意的是generator 库是使用continuations实现的,而在ruby中这个是计算昂贵(computationally expensive,)的过程,因此如果如果有很多的大数字的话,使用generator 将会非常缓慢.

5
2
分享到:
评论

相关推荐

    The Ruby Way--3rd Edition--2015-英文版

    The Ruby Way 第三版(英文版),全书22章,书中包含600多个按主题分类的示例。每个示例都回答了“如何使用Ruby来完成”的问题。 ——Ruby on Rails之父David Heinemeier Hansson倾力推荐!

    The Ruby Way(第2版)

    The Ruby Way(第2版) &lt;br&gt;The Ruby Way assumes that the reader is already familiar with the subject matter. Using many code samples it focuses on "how-to use Ruby" for specific applications, either ...

    THE RUBY WAY(中文版)(第二版)pdf

    《The Ruby Way 第二版》...“《The Ruby Way (第2版)中文版》在阐述元编程(metaprogramming)等方面尤其出类拔萃,而元编程是Ruby最引人注目的方面之一。” ——Ruby on Rails之父David Heinemeier Hansson倾力推荐!

    the ruby way 2ed

    《The Ruby Way 2nd Edition》是一本深入探讨Ruby编程语言的经典著作,旨在帮助读者全面理解和掌握Ruby的精髓。这本书的第二版在2006年出版,由Addison-Wesley出版,作者通过深入浅出的方式,揭示了Ruby语言的强大...

    ruby学习资源(Programming Ruby, Learning Ruby, The Ruby Way)

    内含以下4个文档: 1、Addison.Wesley.The.Ruby.Way.2nd.Edition.Oct.2006.chm 2、O'Reilly.Learning.Ruby.May.2007.chm 3、Programming Ruby 2e.pdf 4、ruby中文文档.chm

    the-ruby-way

    the ruby way the ruby way

    11.5 时间日期the ruby way.rar

    "11.5 时间日期the ruby way"这个主题深入探讨了Ruby中处理时间日期的最佳实践和常见用法。让我们逐一了解这些知识点。 首先,`Time.now`是Ruby中获取当前时间的标准方法。它返回一个`Time`对象,表示自1970年1月1...

    Addison Wesley The Ruby Way 2Nd Edition Oct 2006.pdf(英文版)

    ### Addison Wesley《The Ruby Way》第二版(2006年10月) #### 书籍概览 《The Ruby Way》是由Hal Fulton编写的关于Ruby编程语言的经典著作,该书的第二版出版于2006年10月,由Addison Wesley Professional出版社...

    Ruby-Way.rar_About Language

    《Ruby Way》是由Hal Fulton编写的关于Ruby编程语言的一本著作。这本书深入浅出地探讨了Ruby语言的各种特性,旨在帮助读者理解并掌握这门强大的动态脚本语言。Ruby以其简洁、优雅的语法和强大的元编程能力而备受赞誉...

    the ruby way

    3. **块、proc和lambda**:Ruby中的块、Proc和Lambda是其独特之处,它们提供了强大的函数式编程特性,允许你编写更简洁、更灵活的代码。 4. **方法和元编程**:Ruby允许在运行时动态定义和修改方法,元编程能力使...

    custom_ruby_enumerables:此应用程序是使用用户自定义代码实现内置Ruby枚举器的演示。

    map my_inject :wrench: 内置Ruby :red_circle: 现场演示入门要获取存储库的本地副本,请在您的终端上运行以下命令: $ cd &lt;Enumerables&gt;$ git clone https://github.com/tta2yta/custom_ruby_enumerables.git...

    Addison.Wesley.The.Ruby.Way.2nd.Edition.Oct.2006.chm

    《The Ruby Way》第二版是Addison-Wesley出版社在2006年推出的一本深入浅出的Ruby编程语言教程。这本书专为初学者设计,旨在帮助读者快速掌握Ruby语言的基础和高级特性,从而轻松入门Ruby编程。Ruby是一种动态、开放...

    The Ruby Way

    Ruby is a fully object-oriented, dynamic scripting language which borrows some of the best features from LISP, Smalltalk, Perl, CLU, and other languages, and blends them into a harmonious whole....

    ruby中英文api

    Ruby是一种面向对象的、动态类型的编程语言,以其简洁、优雅的语法著称,尤其适合Web开发,其中Rails框架是其最著名的应用之一。API(Application Programming Interface)是软件开发者用来与操作系统、库或服务进行...

    Ruby学习资料(含参考手册和Programming Ruby)-中文.rar

    "ruby语言基础-p192.pdf"和"the+ruby+way-p136.pdf"同样可能是书籍的部分章节,分别聚焦于Ruby的基础知识和最佳实践。"The Ruby Way"通常指的是高效的、符合Ruby编程风格的代码写法,这对于提升编程技能非常有帮助。...

Global site tag (gtag.js) - Google Analytics