博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ruby array_Ruby中带有示例的Array.zip()方法
阅读量:2532 次
发布时间:2019-05-11

本文共 3291 字,大约阅读时间需要 10 分钟。

ruby array

Array.zip()方法 (Array.zip() Method)

In this article, we will study about Array.zip() Method. You all must be thinking the method must be doing something which is related to zipping values of the Array instance. It is not as simple as it looks. Well, we will figure this out in the rest of our content. We will try to understand it with the help of syntax and demonstrating program codes.

在本文中,我们将研究Array.zip()方法 。 你们都必须认为该方法必须做的事情与Array实例的压缩值有关。 它并不像看起来那么简单。 好吧,我们将在其余内容中解决这个问题。 我们将尝试借助语法并演示程序代码来理解它。

Method description:

方法说明:

This method is a public instance method and defined for the Array class in Ruby's library. This method works in a way that it converts any argument into the Array object and then merges that Array instance with the elements of self array. This results in a sequence of Array.length n-element Array object where n is possibly more than the number of elements. The method will supply "nil" values if the size of any argument is less than the size of the initial Array. If you are providing a block then the block is called for each resulting Array object otherwise an Array of Arrays is returned or you can say that a multi-dimensional Array is returned.

该方法是一个公共实例方法,为Ruby库中的Array类定义。 此方法的工作方式是将任何参数转换为Array对象,然后将该Array实例与self数组的元素合并。 这将导致Array.length个 n元素Array对象的序列,其中n可能大于元素数。 如果任何参数的大小小于初始Array的大小,则该方法将提供“ nil”值。 如果要提供一个块,则为每个结果Array对象调用该块,否则将返回一个Array数组,或者可以说返回了一个多维Array。

Syntax:

句法:

array_instance.zip(arg, ...) -> new_ary    array_instance.zip(arg, ...) { |arr| block } -> nil

Argument(s) required:

所需参数:

This method can take multiple objects as arguments.

此方法可以将多个对象用作参数。

Example 1:

范例1:

=begin  Ruby program to demonstrate zip method=end# array declarationtable = ["fatima","Sabita","Monica","Syresh","Kamish","Punish"]table1 = ["Apple","Banana","Orange","Papaya"]puts "Array zip implementation:"puts "#{
["abc","pqr"].zip(table,table1)}"puts "Array instance : #{table}"

Output

输出量

Array zip implementation:[["abc", "fatima", "Apple"], ["pqr", "Sabita", "Banana"]]Array instance : ["fatima", "Sabita", "Monica", "Syresh", "Kamish", "Punish"]

Explanation:

说明:

In the above code, you can see that we are zipping the Array object with the help of Array.zip() method. This method is a non-destructive method and does not create any change in the actual Array instance.

在上面的代码中,您可以看到我们正在借助Array.zip()方法压缩Array对象。 此方法是一种非破坏性方法,不会在实际的Array实例中创建任何更改。

Example 2:

范例2:

=begin  Ruby program to demonstrate zip method=end# array declarationtable = ["fatima","Sabita","Monica","Syresh","Kamish","Punish"]table1 = ["Apple","Banana","Orange","Papaya"]puts "Array zip implementation:"puts "#{table.zip(["Kuber","Kamesh"],table1)}"puts "Array instance : #{table}"

Output

输出量

Array zip implementation:[["fatima", "Kuber", "Apple"], ["Sabita", "Kamesh", "Banana"], ["Monica", nil, "Orange"], ["Syresh", nil, "Papaya"], ["Kamish", nil, nil], ["Punish", nil, nil]]Array instance : ["fatima", "Sabita", "Monica", "Syresh", "Kamish", "Punish"]

Explanation:

说明:

In the above code, you can see that we are zipping the Array object with the help of the Array.zip() method. This method is a non-destructive method and does not create any change in the actual Array instance.

在上面的代码中,您可以看到我们在Array.zip()方法的帮助下压缩Array对象。 此方法是一种非破坏性方法,不会在实际的Array实例中创建任何更改。

翻译自:

ruby array

转载地址:http://ivtzd.baihongyu.com/

你可能感兴趣的文章
我的Android进阶之旅------>Android嵌入图像InsetDrawable的使用方法
查看>>
Detours信息泄漏漏洞
查看>>
win32使用拖放文件
查看>>
Android 动态显示和隐藏软键盘
查看>>
raid5什么意思?怎样做raid5?raid5 几块硬盘?
查看>>
【转】how can i build fast
查看>>
接口测试-jmeter
查看>>
js便签笔记(5)——Dean Edwards大牛的跨浏览器AddEvent()设计(不知道是不是jQuery事件系统的原型)...
查看>>
重构wangEditor(web富文本编辑器),欢迎指正!
查看>>
null?对象?异常?到底应该如何返回错误信息
查看>>
django登录验证码操作
查看>>
(简单)华为Nova青春 WAS-AL00的USB调试模式在哪里开启的流程
查看>>
UIScrollerView ,UIPageControl混搭使用,添加定时器,无限循环滚动
查看>>
图论知识,博客
查看>>
微信企业号开发之-如何获取secret 序列号
查看>>
2015年最新Android基础入门教程目录(完结版)
查看>>
[原创]一篇无关技术的小日记(仅作暂存)
查看>>
20145303刘俊谦 Exp7 网络欺诈技术防范
查看>>
原生和jQuery的ajax用法
查看>>
iOS开发播放文本
查看>>