什么是 Python 的“zip"的 Ruby 等价物?内置? [英] What is a Ruby equivalent for Python's "zip" builtin?

查看:34
本文介绍了什么是 Python 的“zip"的 Ruby 等价物?内置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于 Python 的内置 zip 函数,是否有任何 Ruby 等价物?如果不是,那么做同样事情的简洁方法是什么?

Is there any Ruby equivalent for Python's builtin zip function? If not, what is a concise way of doing the same thing?

一点上下文:当我试图找到一种干净的方法来进行涉及两个数组的检查时,出现了这个问题.如果我有 zip,我可以这样写:

A bit of context: this came up when I was trying to find a clean way of doing a check involving two arrays. If I had zip, I could have written something like:

zip(a, b).all? {|pair| pair[0] === pair[1]}

我也接受一种干净的方式来做到这一点,没有任何类似于 zip 的东西(其中干净"的意思是没有显式循环").

I'd also accept a clean way of doing this without anything resembling zip (where "clean" means "without an explicit loop").

推荐答案

Ruby 有一个 zip 功能:

Ruby has a zip function:

[1,2].zip([3,4]) => [[1,3],[2,4]]

所以你的代码示例实际上是:

so your code example is actually:

a.zip(b).all? {|pair| pair[0] === pair[1]}

或者更简洁:

a.zip(b).all? {|a,b| a === b }

这篇关于什么是 Python 的“zip"的 Ruby 等价物?内置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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