为什么不each_sli​​ce工作? [英] Why doesn't each_slice work?

查看:139
本文介绍了为什么不each_sli​​ce工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用可枚举#each_sli​​ce。它不会在我的电脑上工作,指出方法是找不到的。

I am trying to use the Enumerable#each_slice. It doesn't work on my computer, stating that method is not found.

我运行红宝石1.8.6(2008-08-11补丁级别287)万向darwin9.0]

I am running ruby 1.8.6 (2008-08-11 patchlevel 287) [universal-darwin9.0]

API: http://ruby-doc.org/core/classes/ Enumerable.html#M003142

例如:

(1..10).each_slice(3) {|a| p a} # I get NoMethodError: undefined method `each_slice' for 1..10:Range

我在做什么错了?

What am I doing wrong?

推荐答案

在红宝石1.8.6,你必须要求枚举(这是STDLIB的组成部分,使用each_sli​​ce之前被合并到1.8.7+核心)。

In ruby 1.8.6 you have to require 'enumerator' (which is part of stdlib and has been merged into core in 1.8.7+) before using each_slice.

可悲的是由STDLIB加入到核心类不提,其中的方法是从红宝石文档列表的方法。

Sadly the ruby-doc lists methods that are added to core classes by stdlib without mentioning where the methods are from.

这篇关于为什么不each_sli​​ce工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆